2014-12-02 28 views
1

我的通知顯示方法:獲取通知的意圖羣衆演員

public static void ShowNotification(int id, String NotifFirstText, 
     String NotifTitle, String NotifeText, int notificon, long when) { 

    try { 
     Context context = ApplicationClass.context; 
     NotificationManager notificationManager = (NotificationManager) context 
       .getSystemService(context.NOTIFICATION_SERVICE); 

     int icon = notificon; 
     CharSequence notiText = NotifFirstText; 
     long meow = when; 

     Notification notification = new Notification(icon, notiText, meow); 
     CharSequence contentTitle = NotifTitle; 
     CharSequence contentText = NotifeText; 
     Intent notificationIntent = new Intent(); 
     String mPackage = "mypackage"; 
     String mClass = ".ActivityShow"; 
     notificationIntent.setComponent(new ComponentName(mPackage, 
       mPackage + mClass)); 
     notificationIntent.putExtra("id", id); 

     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
       | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

     notification.flags = Notification.DEFAULT_LIGHTS 
       | Notification.FLAG_AUTO_CANCEL 
       | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND; 

     PendingIntent contentIntent = PendingIntent.getActivity(G.context, 
       0, notificationIntent,0); 

     notification.setLatestEventInfo(context, contentTitle, contentText, 
       contentIntent); 
     int SERVER_DATA_RECEIVED = id; 
     notificationManager.notify(SERVER_DATA_RECEIVED, notification); 
    } catch (Exception e) { 

     e.printStackTrace(); 
    } 

} 

,這我的活動代碼:

if (getIntent().getExtras() != null) { 
     Toast.makeText(getBaseContext(), 
       getIntent().getExtras().getInt("id") + "", Toast.LENGTH_LONG).show(); 
    } 

我創建了多個通知,併爲所有這些設置不同的ID,但是當我點擊在每個通知我給同一個ID ...

如何解決這個問題?

回答

2

當您使用PendingIntent.getActivity(G.context, 0, notificationIntent,0),演員沒有得到每PendingIntent overview代替:

由於這種行爲的,重要的是要知道當兩個意圖被認爲是用於檢索的PendingIntent的目的是相同的。人們犯的一個常見錯誤是創建多個PenttentIntent對象,其Intents只在其「額外」內容中有所不同,期望每次都得到不同的PendingIntent。這不會發生。用於匹配的意圖部分與Intent.filterEquals定義的部分相同。如果您使用兩個與Intent.filterEquals等效的Intent對象,那麼您將爲它們獲得相同的PendingIntent。

雖然在大多數情況下,你會替換最後0FLAG_UPDATE_CURRENT更新的額外內容,這不利於同時有多個通知的問題,相反,他們說:

如果你真的需要同時激活多個不同的PendingIntent對象(比如用作同時顯示的兩個通知),那麼您需要確保將它們與不同的PendingIntents關聯起來有些不同。

最簡單的方法是將您的id作爲請求代碼(第二個參數)傳入。這確保每個PendingIntent分開管理:

PendingIntent.getActivity(G.context, id, notificationIntent, 0)