1

我有频道组

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

4

0 回答 0