Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable sound in notification (Notification in Android O or above) [duplicate]

Tags:

android

My app requirement is to update the a media style notification upon player state change. Work perfectly before, fires and shows a new media type notification with the mediaSession without sound or vibration.

Problem now: In building a notification channel per Android O requirement, I use the following code to create the notification channel. Then the annoying problem is, every time the media session changes, each notification updates, in Android O now plays a notification sound.

I would like to disable the sound for each new notification, if I don't set a sound, the default sound fires, passing in null in both field doesn't work.

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(
                NOTIFICATION_CHANNEL_ID,
                "SimpleBakingApp Media Notification",
                NotificationManager.IMPORTANCE_LOW
        );

        // Configure the notification channel.
        notificationChannel.setDescription("Channel description");
        notificationChannel.setSound(null,null); // <-- Is there a way to disable sound? null doesn't work
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.enableVibration(false);
        mNotificationManager.createNotificationChannel(notificationChannel);
    }

Extra info, may be relevent

my showNotification() (The method that builds the notification) fires upon player state changes in the Player.EventListener callback, I'm using ExoPlayer v2.

like image 271
Andrew Lam Avatar asked Sep 17 '17 20:09

Andrew Lam


1 Answers

What you're looking for is setOnlyAlertOnce(boolean).

When you're creating your notification, you can set this to true if you only want the first display of that notification (and not subsequent updates to the same notification) to trigger sound and/or vibration.

like image 190
Kevin Coppock Avatar answered Nov 13 '22 08:11

Kevin Coppock