在收到推送通知时添加新通知(而不是替换旧通知) [英] Add a new notification when push notification receives (not replace old one)

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

问题描述

我正在我的应用程序上使用推送通知. 我曾经在推送通知到来时显示通知,如果我发送另一个通知(不清除先前的通知),它将替换新的通知.

I am using push notification on my app. I used to display a notification when a push notification comes, if I send another notification (without clearing the previous notification)it replaces the new 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 notification, I wanted to add it as a new notification.

推荐答案

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

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天全站免登陆