Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android O Notification for Direct reply message

We are migrating to Notification Channel system in Android O, we noticed that once Channel is created, it's properties cannot be changed.

We have the following scenario, - Notification channel is created with,

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

/** * Higher notification importance: shows everywhere, makes noise and peeks. May use full screen * intents. */

  • we have messaging style with history of messages
  • User receives a message in the notification bar - Notification sound plays.
  • User replies to message, we've implemented BroadcastReceiver to receive replied message and update the notification again with the latest message, But due to the channel importance being High, notification sound played again which should not play for better user experience.
  • We tried using addHistoricMessage() for the replied message, which shows the same behavior

Is there any way to prevent Android from playing sound for notification for the replied messages.

code: Channel creation:

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

Notification builder:

    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: same notification builder is used with previous notificationId

like image 390
Sivakumar Chellamuthu Avatar asked Apr 30 '26 09:04

Sivakumar Chellamuthu


1 Answers

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

like image 63
Piotr Brzozowski Avatar answered May 04 '26 09:05

Piotr Brzozowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!