2015-03-19 71 views
2

我有多個通知的問題,而putExtra使用PendingIntentActivitygetStringExtra fom Intent最後返回putExtra,同時生成Notification從activityInventIntent中獲取額外的信息launchMode =「singleTop」與多個通知

讓我首先解釋完整的情況,如果我錯了,讓我正確。

首先我的Activity的(假設它的MainActivitylaunchModesingleTop。我將它設置爲manifest.i.e。

<activity 
    android:name=".MainActivity" 
    android:launchMode="singleTop" 
    ... /> 

現在我產生notifaction使用此代碼,

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    Intent notifyIntent = new Intent(context, 
      MainActivity.class); 
    notifyIntent.putExtra("message", msg); 
    Log.i(TAG, msg); 
    notifyIntent.putExtra("title", notification_title); 
    Log.i(TAG, notification_title); 
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent pendingIntent = PendingIntent.getActivity(
      BeaconService.this, 0, notifyIntent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 
    Notification notification = new Notification.Builder(
      context).setSmallIcon(R.drawable.ic_launcher) 
      .setContentTitle(notification_title).setContentText(msg) 
      .setAutoCancel(true).setContentIntent(pendingIntent).build(); 
    notification.defaults |= Notification.DEFAULT_SOUND; 
    notification.defaults |= Notification.DEFAULT_LIGHTS; 
    notificationManager.notify(NOTIFICATION_ID, notification); 
    NOTIFICATION_ID++; 

我還使用標誌PendingIntent.FLAG_UPDATE_CURRENT。但是,while clicking on any Notification (let's say we've 5 notifactions) it just returns the fifth extra put while generating Notification all top four notification extra are just like lost somewhere

在我MainActivity我也覆蓋onNewIntent一樣,

@Override 
    protected void onNewIntent(Intent intent) { 
     super.onNewIntent(intent); 
     // getIntent() should always return the most recent 
     setIntent(intent); 
     if (intent.hasExtra("message")) { 
      String message = intent.getStringExtra("message"); 
     } 
     if (intent.hasExtra("title")) { 
      String title = intent.getStringExtra("title"); 
     } 
    } 

返回這個演員總是從去年通知的額外費用。我不知道我哪裏錯了?

我也嘗試了一些鏈接,但沒有找到有用的解決方案。

Intent extras being lost when sending with PendingIntent for GCM

請幫助。

回答

1

您並未創建獨特的PendingIntent s。在Stackoverflow上,這個問題肯定有一千個答案。

當調用此:

PendingIntent pendingIntent = PendingIntent.getActivity(
     BeaconService.this, 0, notifyIntent, 
     PendingIntent.FLAG_UPDATE_CURRENT); 

第二次,它會找到第一個PendingIntent並返回對它的引用(Android將不會再創建一個)。此外,由於您設置了FLAG_UPDATE_CURRENT,因此Android會修改底層Intent,方法是覆蓋舊Intent中的附加內容,並附帶您在新的Intent中提供的附加內容。這就是爲什麼它總是隻提供一個IntentIntent總是有最後一組額外。

您需要使每個PendingIntent都是唯一的。有很多方法可以做到這一點(設置不同的附加組件不是其中一種方法)。嘗試提供一個唯一編號作爲requestCode參數PendingIntent.getActivity()

+0

爲PendingIntent.getActivity()提供了一個唯一的編號作爲requestCode參數,謝謝! – 2015-03-25 05:44:03

1

@David Wasser謝謝你的回答!

我只是想分享一段代碼,我已經使用並解決了我的問題。

提供一個唯一的編號爲requestCode參數爲PendingIntent.getActivity()是多通知的正確解決方案,而launchmode="singleTop"

To provide unique代碼爲requestCode什麼,我所做的是,放置NOTIFICATION_IDrequestCode即,

PendingIntent pendingIntent = PendingIntent.getActivity(
     BeaconService.this, NOTIFICATION_ID, notifyIntent, 
     PendingIntent.FLAG_UPDATE_CURRENT); 

我也發現了同樣的solution at here

這就是perfect unique requestCode in my case。我希望這會對其他人很有幫助,誰是正在面臨的問題。