gpt4 book ai didi

直接回复消息的 Android O 通知

转载 作者:太空狗 更新时间:2023-10-29 13:03:45 24 4
gpt4 key购买 nike

我们正在迁移到 Android O 中的 Notification Channel 系统,我们注意到一旦创建了 Channel,它的属性就无法更改。

我们有以下场景,- 通知 channel 是通过创建的,

NotificationChannel channel = new NotificationChannel(channelId, name, NotificationManager.IMPORTANCE_HIGH);

/** * 更高的通知重要性:无处不在,发出声音和偷看。可以使用全屏 * Intent 。 */

  • 我们有带有消息历史的消息传递风格
  • 用户在通知栏中收到一条消息 - 播放通知声音
  • 用户回复消息,我们已经实现了 BroadcastReceiver 来接收回复的消息并用最新的消息再次更新通知,但是由于 channel 重要性为高,通知声音再次播放这不应该发挥更好的用户体验。
  • 我们尝试对回复的消息使用 addHistoricMessage(),它显示了相同的行为

有什么方法可以防止 Android 播放回复消息通知的声音。

代码: channel 创建:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = title;
NotificationChannel channel = new NotificationChannel(MESSAGE_CHANNEL, name, NotificationManager.IMPORTANCE_HIGH);
android.app.NotificationManager notificationManager = context.getSystemService(android.app.NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}

通知生成器:

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, MESSAGE_CHANNEL);
NotificationCompat.MessagingStyle style = new NotificationCompat.MessagingStyle(displayName)
.setConversationTitle(conversation.isGroup() ? conversation.getTitle(context) : null);
style.addMessage(message, timestamp, sender);
.
.
.
.
builder.setStyle(style);
builder.setShowWhen(true);
builder.setGroup(MESSAGING_GROUP_LABEL);
builder.setColor(ContextCompat.getColor(context, conversation.getColorSet().getPrimaryColorId()));
setVisibility(builder);
builder.setAutoCancel(true);
setPriority(builder, NotificationCompat.PRIORITY_MAX);
setCategory(builder, Notification.CATEGORY_MESSAGE);
setSmallIcon(builder, R.drawable.ic_stat_ic_notif);
NotificationManagerCompat.from(context).notify(conversation.getConversationId(), notificationId, builder.build());

消息回复接收者:相同的通知构建器与之前的 notificationId 一起使用

最佳答案

如果现有通知被修改为额外的消息,那么您可以在同一个构建器上使用:

notificationBuilder.setOnlyAlertOnce(true);

这甚至适用于 Android O 通知 channel 中的通知。它会阻止振动、声音,但如果 channel 设置为紧急 (IMPORTANCE_HIGH),它仍会查看。

这个可能的解决方案是在这篇文章中找到的,它还有其他关于处理“消息历史记录”通知的好主意:Android: How to use MessagingStyle for notifications without caching messages

关于直接回复消息的 Android O 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51549456/

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