在Android中,如何从同一个应用程序发送不同类型的推送通知

时间:2016-03-16 00:11:18

标签: android push-notification google-cloud-messaging

在Android中,当您获得相同类型的推送通知时,最新的推送通知会替换通知屏幕中的前一个推送通知,这很可能会阻止混乱。但是,我在一些应用程序中看到,我从同一个应用程序中获得了不替换先前通知的单独通知。如何编写我的应用程序来执行此操作?

1 个答案:

答案 0 :(得分:2)

为了在状态栏上显示多个通知,您只需为所需的每个通知设置唯一的通知ID。如果您发送两个具有相同ID的通知,则通知管理器会使用最新的通知替换最旧的通知。看看Notification Manager的通知方法。

以下是如何发送通知的示例:

Notification notification = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(alarmSound)
            .setPriority(Notification.PRIORITY_MAX)
            .build();

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(notificationId, notification);

确保notificationId是唯一的,否则它将用相同的ID替换最旧的通知。