我有频道组
NotificationChannel notificationChannel = new NotificationChannel(GENERAL_CHANNEL_ID,
GENERAL_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.enableLights(true);
notificationChannel.setShowBadge(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationManager.createNotificationChannel(notificationChannel);
notificationManager.createNotificationChannelGroup(new NotificationChannelGroup(group, groupName));
notificationManager.notify(code, getNotificationSingle(intent, title, message, group).build());
notificationManager.notify(groupCode, getNotificationGroup(intent, title, message, group).build());
如果要大于2,我想禁用通知声音,但是我仍然可以听到通知声音。
@TargetApi(26)
public Notification.Builder getNotificationGroup(PendingIntent intent, String title, String body, String group) {
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
return new Notification.Builder(getApplicationContext(), GENERAL_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_qlub_logo_trans)
.setColor(colorForOreo())
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setGroup(group)
.setGroupSummary(true)
.setVibrate(new long[]{250, 300, 800})
.setContentIntent(intent);
}
@TargetApi(26)
public Notification.Builder getNotificationSingle(PendingIntent intent, String title, String body, String group) {
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
return new Notification.Builder(getApplicationContext(), GENERAL_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_qlub_logo_trans)
.setColor(colorForOreo())
.setContentTitle(title)
.setContentText(body)
.setGroupAlertBehavior(Notification.GROUP_ALERT_SUMMARY)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setGroup(group)
.setVibrate(new long[]{250, 300, 800})
.setContentIntent(intent);
}
添加Notification.GROUP_ALERT_SUMMARY
无济于事。也许我试图以错误的方式对它们进行分组,但是如果该组中有2个或更多项目,如何禁用声音?
编辑
将.setSound()
从Notification.Builder
移到NotificationChannel
实例并设置setOnlyAlertOnce(true)
解决了这个问题。