push-notification - 通知分组在 Android 8.1 中不起作用
问题描述
我正在使用 FCM 在 8.1 版本的 Android 设备上发送通知。我正在尝试实现通知的分组,并且我已经实现了。
每当第一个通知在状态栏中并且第二个通知到达时,第一个通知就会消失。从病房的第二个通知开始,所有通知都按预期显示在组中。
如何解决?
在下面发布代码狙击手。
if ("FirstTime".equals(new SharedPrefsOperations(this).getPreferencesData("ActiveNotifs"))) {
// I AM EXECUTING THIS ONLY AT FIRST TIME , WHEN THE FIRST NOTIFICATION ARRIVES.
String tempData = new SharedPrefsOperations(this).getPreferencesData("ActiveNotifs");
Notification notification = new NotificationCompat.Builder(this, "MYAPP")
.setSmallIcon(R.mipmap.ic_static_notif)
.setContentTitle(content.getTitle())
.setContentText(tempMsg)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setGroup(notifGroup)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setChannelId(channel_id)
.setStyle(inboxStyle)
.setGroupSummary(true)
.build();
notificationManager.notify(id, notification);
new SharedPrefsOperations(this).storePreferencesData("ActiveNotifs", "1");
} else {
// I AM EXECUTING THIS FROM SECOND NOTIFICATION ONWARDS
String tempData = new
SharedPrefsOperations(this).getPreferencesData("ActiveNotifs");
Notification notification = new NotificationCompat.Builder(this, "MYAPP")
.setSmallIcon(R.mipmap.ic_static_notif)
.setContentTitle(content.getTitle())
.setContentText(tempMsg)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setGroup(notifGroup)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setChannelId(channel_id)
.setStyle(inboxStyle)
.build();
notificationManager.notify(id, notification);
new SharedPrefsOperations(this).storePreferencesData("ActiveNotifs", "1");
}
解决方案
如果您使用相同id
的通知,它将不起作用。你需要为此使用不同id
的
Android 8.0 (Oreo) 以上的更新通知渠道是强制性的。所以像这样添加通知通道。这应该在通知之前完成。
val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// Android O requires a Notification Channel.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = context.getString(R.string.app_name)
// Create the channel for the notification
val mChannel = NotificationChannel("channel_id_sms", name, NotificationManager.IMPORTANCE_DEFAULT)
// Set the Notification Channel for the Notification Manager.
mNotificationManager.createNotificationChannel(mChannel)
}
推荐阅读
- linux - 安装了 create-react-app 并且无法更改文件夹结构
- docker - 如何将 docker 容器连接到运行在 127.0.0.1 的服务器
- java - AWS Kinesis Client Java:在 Stream 中设置 TRIM_HORIZON 位置不起作用
- c - 如何动态居中对齐 char * 字符串中的文本以适合 C 中的总共 16 个空格?
- load-testing - 为什么连接超时和响应超时在 JMeter 中不起作用?
- python - Sphinx 项目的自动文档
- r - 用闪亮的方式美化传单地图?
- sql-server - 组合两组不同的查询
- node.js - TypeORM 注释的参数
- php - 仅显示数据库中的最后一条记录