2

我在這樣的通知欄上顯示通知。我正在那些,但我不能顯示多個通知,一次只有一個。當一個新的來臨,前一個去。問題是什麼?在收到gcm通知時顯示多個通知

public void createNotificationRecever(Context context, String payload) { 
     Toast.makeText(context, commentor + "commented on your post " ,Toast.LENGTH_LONG).show(); 
     //New message received 
     NotificationManager notificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification = new Notification(R.drawable.flag, 
       payload, System.currentTimeMillis()); 
     // Hide the notification after its selected 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

     Intent intent = new Intent(context, MessageReceivedActivity.class); 
     intent.putExtra("id", groupid); 
     intent.putExtra("userid", text); 
     intent.putExtra("cname", groupname); 
     intent.putExtra("image", ""); 

     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
       intent, 0); 
     notification.setLatestEventInfo(context, "Message", 
       payload, pendingIntent);   
     notificationManager.notify(0, notification); 


    }} 

回答

5

根據您需要多少通知,有幾種解決方案。你可以添加一個增加到你的通知的ID,所以它會有不同的名稱,因此不會用相同的ID替換另一個ID,或者如果你只需要兩個通知最大值,那麼只需創建另一個字符串/變量名稱的通知您正在使用。

看一看這裏的ID增量:

Android: Managing Multiple Notifications

如果你只需要一個第二或第三通知改變你的字符串是這樣的,例如:

public void createNotificationRecever(Context context2, String payload2) { 
    Toast.makeText(context2, commentor + "commented on your post " ,Toast.LENGTH_LONG).show(); 
    //New message received 
    NotificationManager notificationManager = (NotificationManager) context2 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification2 = new Notification(R.drawable.flag, 
      payload2, System.currentTimeMillis()); 
    // Hide the notification after its selected 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    Intent intent = new Intent(context2, MessageReceivedActivity.class); 
    intent.putExtra("id", groupid2); 
    intent.putExtra("userid", text2); 
    intent.putExtra("cname", groupname2); 
    intent.putExtra("image", ""); 

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
      intent, 0); 
    notification.setLatestEventInfo(context, "Message", 
      payload, pendingIntent);   
    notificationManager.notify(0, notification2); 


}} 

希望你得到了這個要點,它可以幫助你。

5

使用此代碼它將帶來多個通知列表 int NOTIFICATION_ID = 0; notificationManager.notify(NOTIFICATION_ID, notification2); NOTIFICATION_ID++

0

@SunnySonic,你需要使用最新的穩定版本的「Android支持庫」。

要下載最新的「Android支持庫」的穩定版本, 轉到SDK管理器 - >其他 - >點擊Android支持庫並更新它。

並轉到build.gradle和「依賴關係」下,更改版本。

dependencies { 
    compile 'com.android.support:support-v4:22.1.1' //<-change this 
    compile files('libs/bolts-android-1.1.4.jar') 
    compile files('libs/gcm.jar') 

} 
0

您可以在notify方法中生成隨機數作爲NotificationId。

notificationManager.notify(generateRandom(),notificationBuilder.build());

public int generateRandom() 
{ 
    Random rn = new Random(); 
    int n = maximum - minimum + 1; 
    int i = rn.nextInt() % n; 
    return minimum + i; 

}