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

5
我是一名有用的助手,可以为您翻译文本。
我正在尝试实现捆绑通知。经过大量教程和博客的学习,我了解到我必须生成两个通知,一个是常规通知,另一个是摘要通知。我按照这些博客文章中所述的一切去做,一切似乎都有效。但是在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);

我应该如何解决双声问题?非常感谢您的帮助!

你好,你有找到任何解决方案吗? - Jinu
没有,我还没有得到任何解决方案! - jamael
我得到了解决方案。 - Jinu
能否请您分享解决方案? - jamael
这是我的代码 https://gist.github.com/jinujayakumar/0d16a7272543c1bd837480a549ad717c - Jinu
2个回答

1
另一个解决方案是在构建器中设置:

setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY)

请看

查看文档


谢谢你的解决方案。由于我目前没有积极地在此工作,所以我无法确定这个解决方案是否有效。等我有机会尝试后再告诉你。 - jamael

0

我在Android 8+上遇到了同样的问题,通过创建低优先级的额外通道来解决了它:

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

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

new NotificationCompat.Builder(context, silentChannelId)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接