推送通知:不替换旧消息

本文关键字:替换 消息 通知 | 更新日期: 2023-09-27 18:18:31

我做了一个推送通知到Android,但是新消息的内容会取代旧的。如何解决这个问题?

以下代码:

push.QueueNotification(new GcmNotification().ForDeviceRegistrationId("APA91bGZpTp6n3W29RjV_yN3ulNSnP2EAmqccfLQTRdBva_9D7Yy8S-uLqqZ0-gqEn0q2Xwb5o7-v0KEugGjYaX05--8UW17RT4piF5rU0uCjnIwzEc_rwiuo-KY-r1pRe4_ElD65bBExXn2KnbT0iwHTKZmDOqqXw")
.WithJson("{'"notId'":'"1'",'"type'":'"a'",'"caseid'":'"123'",'"contract'":'"a1'",'"title'":'"Message: Type a'",'"message'":'"[a1] '"}")
.WithTag("1"));

push.QueueNotification(new GcmNotification().ForDeviceRegistrationId("APA91bGZpTp6n3W29RjV_yN3ulNSnP2EAmqccfLQTRdBva_9D7Yy8S-uLqqZ0-gqEn0q2Xwb5o7-v0KEugGjYaX05--8UW17RT4piF5rU0uCjnIwzEc_rwiuo-KY-r1pRe4_ElD65bBExXn2KnbT0iwHTKZmDOqqXw")
.WithJson("{'"notId'":'"2'",'"type'":'"b'",'"caseid'":'"456'",'"contract'":'"b1'",'"title'":'"Message: Type b'",'"message'":'"[b1] '"}")
.WithTag("2"));

推送通知:不替换旧消息

关键是为要连接在一起的所有消息设置通知id。如下所示:

mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
mNotifyBuilder = new NotificationCompat.Builder(this)
    .setContentTitle("New Message")
    .setContentText("You've received new messages.")
    .setSmallIcon(R.drawable.ic_notify_status)
numMessages = 0;
// Start of a loop that processes data and then notifies the user
...
mNotifyBuilder.setContentText(currentText)
    .setNumber(++numMessages);
// Because the ID remains unchanged, the existing notification is
// updated.
mNotificationManager.notify(
        notifyID,
        mNotifyBuilder.build());

在这个文档链接上找到更多信息:http://developer.android.com/guide/topics/ui/notifiers/notifications.html更新