gpt4 book ai didi

android - Android O 上 bundle 通知的双重通知声音

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:10:06 24 4
gpt4 key购买 nike

我正在尝试实现 bundle 通知。在浏览了大量教程和博客之后,我了解到我必须生成两个通知。一种是定期通知,另一种是摘要通知。我遵循了那些博客文章中所述的所有内容。一切似乎都有效。但是我在 Android O 上的每个通知都会收到双重通知声音。无论如何我都无法解决这个问题。我搜索了其他人可能遇到的任何类似问题。但我没有发现任何有用的东西。

下面是一些生成通知的代码 fragment

定期通知

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);

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

最佳答案

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

setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY)

参见 documentation

关于android - Android O 上 bundle 通知的双重通知声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47249633/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com