【发布时间】:2020-05-24 02:24:58
【问题描述】:
我想将收到的多个推送通知分组到我的应用程序(例如 Gmail)。 下图将描述要求。
我浏览过很多教程,包括开发者网站https://developer.android.com/training/notify-user/group
但没有运气。所有收到的通知都显示为单独的通知。
下面是我的代码 sn-p。
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
// put the new message onto the event bus so that interested activities can subscribe to it
// TODO[tim] we should probably look at the content of the message and send different events
// onto the bus for different message types.
int notificationId = (int) SystemClock.currentThreadTimeMillis();
createNotificationChannel();
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, ANDROID_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher) // notification icon
.setContentTitle(getTitle()) // title
.setContentText("Vyn processed and ready to play") // body message
.setContentIntent(getVynPlayIntent(getIntentExtraValues(), notificationId))
.setAutoCancel(true) // clear notification when clicked
.setGroup(GROUP_KEY_VYNS)
.setGroupSummary(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
if (builder != null) {
getManager().notify(notificationId, builder.build());
}
}
private static final String ANDROID_CHANNEL_ID = "notifications.ANDROID_CHANNEL_ID";
private static final String GROUP_KEY_VYNS = "notifications.GROUP_KEY_VYNS";
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "channel_name";
String description = "channel_description";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(ANDROID_CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
getManager().createNotificationChannel(channel);
}
}
private NotificationManagerCompat mManagerCompat;
public NotificationManagerCompat getManager() {
if (mManagerCompat == null) {
mManagerCompat = NotificationManagerCompat.from(mContext);
}
return mManagerCompat;
}
请帮我找出这段代码中遗漏的地方,如果你能确定的话。
【问题讨论】:
标签: android push-notification firebase-cloud-messaging apple-push-notifications grouping