11

我正在開發一個應用程序,其中我實現了推送通知功能。 我的的onMessage碼 - GCMIntentService.java是:Android:在應用程序的設備中管理多個推送通知

@Override 
protected void onMessage(Context context, Intent data) { 
    String message = data.getExtras().getString("message"); 

    displayMessage(context, message); 
    generateNotification(context, message); 
} 

而且generateNotification的代碼 -

private static void generateNotification(Context context, String message) { 
    int icon = R.drawable.ic_launcher; 
    long when = System.currentTimeMillis(); 
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(icon, message, when); 

    String title = context.getString(R.string.app_name); 

    Intent notificationIntent = new Intent(context, GCMMessageView.class); 

    notificationIntent.putExtra("message", message); 
    // set intent so it does not start a new activity 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0); 

    notification.setLatestEventInfo(context, title, message, intent); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    // Play default notification sound 
    notification.defaults |= Notification.DEFAULT_SOUND; 

    // Vibrate if vibrate is enabled 
    notification.defaults |= Notification.DEFAULT_VIBRATE; 
    notificationManager.notify(0, notification); 
} 

此代碼工作正常。我收到推送消息和通知。

=>但是當我發送更多的消息時,通知就會被寫入。而且我只能看到狀態欄中的最後一個通知。

應用的需要是 - 如果我一個以上發送通知,然後都應該在狀態欄中顯示

所以請指導我該怎麼做我的代碼。

回答

7

儘量把不同的通知ID(即NID)的意圖,而是通知0每個新的通知,這將防止過度寫你的通知

PendingIntent intent = PendingIntent.getActivity(context, nid,notificationIntent, 0); 

notificationManager.notify(nid, notification);

希望這會幫助你

1

你需要更新你的Notification ID當你開火Notification. in

notificationManager.notify(ID, notification); 

怎麼樣?

要通過呼叫NotificationManager.notify(ID, notification).要改變這種通知設置通知,因此它可以是不同的,有一個通知ID問題,它一旦你已經發出,創建一個NotificationCompat.Builder對象,從它建立一個Notification對象,發行NotificationDifferent ID您以前不使用。

如果以前的通知仍然可見,系統會從Notification object的內容更新它。如果先前的通知已被解除,則會創建新的通知。

欲瞭解更多信息,請訪問官方docs

2

你好,你需要在通過獨特的通知ID通知方法。 下面是通唯一的通知ID代碼:

//"CommonUtilities.getValudeFromOreference" is the method created by me to get value from savedPreferences. 
String notificationId = CommonUtilities.getValueFromPreference(context, Global.NOTIFICATION_ID, "0"); 
int notificationIdinInt = Integer.parseInt(notificationId); 

notificationManager.notify(notificationIdinInt, notification); 

// will increment notification id for uniqueness 
notificationIdinInt = notificationIdinInt + 1; 
CommonUtilities.saveValueToPreference(context, Global.NOTIFICATION_ID, notificationIdinInt + ""); 
//Above "CommonUtilities.saveValueToPreference" is the method created by me to save new value in savePreferences. 

讓我知道如果您需要更詳細的信息或任何查詢。:)

1

獲取隨機數:

Random random = new Random(); 
int m = random.nextInt(9999 - 1000) + 1000; 

相反的:

notificationManager.notify(0, notification); 

使用:

notificationManager.notify(m, notification); 
0
notificationManager.notify(Different_id_everytime, notification); 
相關問題