Android O上捆绑通知的双重通知声音

时间:2017-11-12 13:54:42

标签: android notifications android-8.0-oreo

我正在尝试实施捆绑通知。经过大量的教程和博客后,我知道我必须生成两个通知。一个是定期通知,另一个是摘要通知。我按照这些博客文章中的说明进行了操作。一切似乎都有效。但是我在Android O上的每个通知都会收到双重通知声音。无论如何,我无法修复此问题。我搜索过其他人可能面临的任何类似问题。但我没有找到任何有用的东西。

以下是生成通知的一些代码段

常规通知

public Notification getSmallNotification(String channelId, String title, String body, Intent intent) {
    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    mContext,
                    ID_SMALL_NOTIFICATION,
                    intent,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );

    NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, channelId);
    builder.setTicker(title)
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setContentIntent(resultPendingIntent)
            .setContentTitle(title)
            .setContentText(body)
            .setSmallIcon(R.drawable.ic_gw_notification)
            .setColor(ContextCompat.getColor(mContext, R.color.color_bg_splash))
            .setGroup(channelId);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
        builder.setDefaults(Notification.DEFAULT_SOUND);
    }

    Notification notification = builder.build();

    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    return notification;
}

摘要通知

public Notification getSummaryNotification(String channelId, String title, String body) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, channelId)
            .setContentTitle(title)
            .setContentText(body)
            .setSmallIcon(R.drawable.ic_gw_notification)
            .setColor(ContextCompat.getColor(mContext, R.color.color_bg_splash))
            .setShowWhen(true)
            .setGroup(channelId)
            .setGroupSummary(true);

    return builder.build();
}

然后我同时调用这两个函数

notification = gwNotificationManager.getSmallNotification(channelId, title, body, intent);
notificationUtils.getManager().notify(channelId, (int) uniqueId, notification);
Notification summaryNotification = gwNotificationManager.getSummaryNotification(channelId, groupTitle, groupBody);
notificationUtils.getManager().notify(channelId, 0, summaryNotification);

如何解决双音问题?任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

另一种解决方案是在构建器中设置

setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY)

请参见documentation

答案 1 :(得分:0)

我在Android 8+上遇到同样的问题,我通过创建一个低重要性的附加频道解决了这个问题:

manager.createNotificationChannel(new NotificationChannel(silentChannelId, name, NotificationManager.IMPORTANCE_LOW));

然后使用此渠道创建摘要通知:

new NotificationCompat.Builder(context, silentChannelId)