在收到推送通知时添加新的通知(而不是替换以前的通知) [英] Add a new notification when push notification is received (not replace the previous)

查看:74
本文介绍了在收到推送通知时添加新的通知(而不是替换以前的通知)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用了推送通知.推送通知传递后,我需要显示一个通知.如果我发送另一个通知(不清除前一个通知),它将替换旧的通知.

I am using push notifications in my app. I need to display a notification when a push notification delivered. If I send another notification (without clearing the previous notification), it will replace the old notification.

这是我使用的代码:

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

int icon = R.drawable.ic_launcher;
CharSequence tickerText = "New notification Pending";
long time = System.currentTimeMillis();

Notification notification = new Notification(icon, tickerText, time);
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;

// Context context = getApplicationContext();
CharSequence contentTitle = "Notifications";
CharSequence contentText = newMessage;
Intent notificationIntent = new Intent(this, LoginActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
        notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText,
        contentIntent);
mNotificationManager.notify(1, notification);

但是我不想替换通知,我想将其添加为新通知.

But I don't want to replace the notification, I want to add it as a new notification.

推荐答案

您每次都需要提供一个不同的ID作为通知ID.最好的方法是将ID字段发送到GCM,然后可以通过GCMIntentService的onMessage()方法中的 Intent.getExtras().getInt()进行访问.

You need to supply a different ID as the notification ID each time. The best approach would be to send an ID field to GCM which can be then accessed via Intent.getExtras().getInt() in your GCMIntentService's onMessage() method.

如果这不可能,我建议使用类似(int)Math.random()* 10 的东西来生成一个随机整数作为您的通知ID.这将(部分)确保您的通知不会彼此替换.

If this is not possible, I'd suggest using something like (int)Math.random()*10 to generate a random integer number as your notification ID. This will (partially) ensure that your notifications will not replace each other.

这篇关于在收到推送通知时添加新的通知(而不是替换以前的通知)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆