3

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

我们有以下场景, - 创建通知通道,

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

/** * 更高的通知重要性:到处显示,发出噪音和偷看。可以使用全屏 * 意图。*/

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

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

代码:频道创建:

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

messageRepliedReceiver:相同的通知构建器与先前的通知 ID 一起使用

4

1 回答 1

3

If existing notification is amended with extra message then you can use on the same builder:

notificationBuilder.setOnlyAlertOnce(true);

This works even for Notifications in Android O Notification Channels. It will prevent vibration, sound, but it will still peek if Channel setting was Urgent (IMPORTANCE_HIGH).

This possible solution was found in this post that has other great ideas about handling "history of messages" notification: Android: How to use MessagingStyle for notifications without caching messages

于 2018-09-18T17:01:32.687 回答