Android通知频道。为组禁用声音

时间:2018-08-21 11:10:58

标签: android notifications notification-channel

我有频道组

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)解决了这个问题。

0 个答案:

没有答案