1

Is there a way to only play the notification sound once for a duplicate notification that comes through?

Sometimes our users accidentally send more then one of the same notification and on the second one would not like the annoying sound to occur for a duplicate notification in Android. I can't find a setting that handles this.

Mike Flynn
  • 22,342
  • 54
  • 182
  • 341

2 Answers2

0

I don't think there is a function to handle this on his own but you could write it if you have unique ID for each notification(so that a duplicate would have the same id).

I'll explain better: when you receive a new message in your onMessageReceived() function you check and see if a notification with the same ID is present. If so the new one is a duplicate otherwise it is a new one for real.

Here a possible way of checking the notification Is it possible to check if a notification is visible or canceled?

Community
  • 1
  • 1
0

Assuming you know to identify what is a "duplicate notification" this code should do the trick:

private Notification buildNotification(Context context, int icon, String title, String message, boolean isDuplicate) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setSmallIcon(icon)
            .setContentTitle(title)
            .setContentText(message)
            // Decide if you want a sound or not based on whether it is duplicate
            .setSound(isDuplicate ? null : RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); 

    return builder.build();
}
Doron Yakovlev Golani
  • 5,188
  • 9
  • 36
  • 60