我正在开发一个具有两个前台服务的Android APP,并且希望将通知分组到一个Group中。有可能这样做吗?我已经使用NotificationCompat.Builder setGroup(String groupKey)启用组通知,但是它不起作用。
NotificationHelper.kt
enum ChannelType{ ID1, ID2}
fun getNotificationChannelID(context: Context, channelType: ChannelType) : String? {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channelID = channelType.channelID
val description = channelType.reourceID
val channel = NotificationChannel(channelID, description,
NotificationManager.IMPORTANCE_HIGH);
val mNotificationManager = mContext.getSystemService(Context.NOTIFICATION_SERVICE)
as android.app.NotificationManager
mNotificationManager.createNotificationChannel(channel)
return channelID
}
return null;
}
fun createBuilder(context: Context, channelId: String, title: String, content: String,
ongoing: Boolean, autoCancel: Boolean,
style: NotificationCompat.Style, @DrawableRes icon: Int,
priority: Int, vibratePattern: LongArray): NotificationCompat.Builder {
val builder = NotificationCompat.Builder(context, channelId)
builder.setSmallIcon(icon)
.setContentTitle(title)
.setContentText(content)
.setAutoCancel(autoCancel)
.setOngoing(ongoing)
.setGroup(GROUP_KEY_NOTIF)
.setStyle(style)
.setGroupSummary(true)
return builder
}
}
来自服务1
onStartCommand() {
var channelID = getNotificationChannelID(context, ChannelType.ID1)
startForeground(NOTIFICATION_ID1,createBuilder(context,
channelID,
"title",
"description",
ongoing = false,
autocancel = true,
someIcon,
BigTextStyle,
priority, vibratePattern )
.build());
}
来自服务2
onStartCommand() {
var channelID = getNotificationChannelID(context, ChannelType.ID1)
startForeground(NOTIFICATION_ID1,createBuilder(context,
channelID,
"title",
"description",
ongoing = false,
autocancel = true,
someIcon,
BigTextStyle,
priority, vibratePattern)
.build());
}
我错过了什么吗?我也将ongoing
设置为false
,结果仍然没有变化。
先谢谢了。