在 Android 中,如何从同一应用发送不同类型的推送通知
In Android, how do I send different types of push notification from the same app
在 Android 中,当您收到相同类型的推送通知时,最新的推送通知会替换通知屏幕中的上一条,这主要是为了防止混乱。但是,我看到在某些应用程序中,我会从同一个应用程序收到单独的通知,这些通知不会替换以前的通知。我该如何对我的应用程序进行编程才能做到这一点?
为了在状态栏上有多个通知,您所要做的就是为您想要的每个通知设置一个唯一的通知 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 的最旧通知。
在 Android 中,当您收到相同类型的推送通知时,最新的推送通知会替换通知屏幕中的上一条,这主要是为了防止混乱。但是,我看到在某些应用程序中,我会从同一个应用程序收到单独的通知,这些通知不会替换以前的通知。我该如何对我的应用程序进行编程才能做到这一点?
为了在状态栏上有多个通知,您所要做的就是为您想要的每个通知设置一个唯一的通知 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 的最旧通知。