Android 收到gcm通知时显示多个通知
我在通知栏上显示通知,如下所示。我正在收到这些通知,但我无法在那里显示多个通知,一次只能显示一个。新的来了,前一个就走了。有什么问题吗Android 收到gcm通知时显示多个通知,android,google-cloud-messaging,android-notifications,android-notification-bar,Android,Google Cloud Messaging,Android Notifications,Android Notification Bar,我在通知栏上显示通知,如下所示。我正在收到这些通知,但我无法在那里显示多个通知,一次只能显示一个。新的来了,前一个就走了。有什么问题吗 public void createNotificationRecever(Context context, String payload) { Toast.makeText(context, commentor + "commented on your post " ,Toast.LENGTH_LONG).show();
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);
}}
根据您需要的通知数量,有几种解决方案。您可以在通知中添加一个递增的id,使其具有不同的名称,因此不会用相同的id替换另一个,或者如果您最多只需要两个通知,则只需使用您正在使用的字符串/变量的不同名称创建第二个通知 在此处查看ID增量: 如果您只需要第二次或第三次通知,请将字符串更改为以下内容,例如:
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);
}}
我希望您能了解要点,并对您有所帮助。使用此代码,它将在列表中显示多个通知 int通知_ID=0;
notificationManager.notify(通知ID,notification2);
通知单++
@SunnySonic,您需要使用最新稳定版本的“Android支持库”
下载最新稳定版本的“Android支持Libraray”,
转到SDK管理器->附加->点击Android支持库并更新它
转到build.gradle并在“dependencies”下更改版本
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')
}
依赖项{
编译'com.android.support:support-v4:22.1.1'/您可以在notify方法中生成随机数作为NotificationId
notificationManager.notify(generateradom(),notificationBuilder.build())
public int generateRandom()
{
Random rn = new Random();
int n = maximum - minimum + 1;
int i = rn.nextInt() % n;
return minimum + i;
}