4

I have a foreground service that shows a progress notification and if it is finished, the notification is reused as a normal notification to show the service's result

I allow the user to define inside my app if the final notification is silent or not. So here is what I do:

// 1) Create notifcation channel in my app, once only
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
notificationChannel.enableLights(false);
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);

// 2) Init notification
notificationBuilder = new NotificationCompat.Builder(this, notificationChannelId);
notificationBuilder.setAutoCancel(false);
notificationBuilder.setOngoing(true);
notificationBuilder.setTicker(ticker);
notificationBuilder.setContentText(title);

// 3) Updating the notification to show the services progress
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(subTitle);

// 4) preparing the final notification
if (!MainApp.getPrefs().silentNotifications()) {
    if (globalError || updated > 0 || prepared > 0 || contactsWithError.size() > 0) {
        notificationBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
        notificationBuilder.setVibrate(new long[]{0, 500});
        notificationBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
    } else {
        notificationBuilder.setVibrate(new long[]{});
        notificationBuilder.setSound(null);
    }
}

Problems

  • an android oreo user told me, the notification is making a sound/vibrates on each update
  • the same user told me, after a restart, the sound/vibration is gone

What I want

  • I want that the notification channel vibrates and plays a sound by default (if the user did not change this in the android settings for my channel)
  • I want to dynamically decide that I don't play sounds/vibrate even if sound/vibration is enabled
  • If the user disables sound/vibration for my channel, I'm fine, than I won't play a sound nor vibrate at any time

How am I supposed to achieve that?

Edit - one solution is following:

Use two channels:

  • one for updating the notification
  • one for the final result notification.

This looks weird to me, I would assume that I can use one channel, set it up with sounds by default and use this channel. I should be able to define myself to play a sound or not, if the user allows me to play a sound (seems like I'm forced to play a sound currently). Otherwise I never play a sound of course. It looks like currently you need two channels for every service, that shows progress in the notification and plays a sound by default if finished. That's bad to explain to the user because you need two channels for the same action, on for the progress and one for the final result...

prom85
  • 16,896
  • 17
  • 122
  • 242
  • 1
    Use two different notification channels (with different sound/vibration settings) on Android 8.0+, where your "allow the user to define inside my app if the final notification is silent or not" logic drives which channel to use. – CommonsWare Nov 13 '17 at 11:42
  • 1
    This way, I have to tell the user the differences between the 2 channels. I thought, if the user allows me to play sounds, I should be able to not play a sound as well and if this is somehow possible, one channel would be enough... But I may do what you suggest – prom85 Nov 13 '17 at 11:54

1 Answers1

0

I was annoyed with the sound of my notifications. I did this.

.setSound(Uri.parse("android.resources://" + getPackageName() + "/" + R.raw.silence))

and this

.setOnlyAlertOnce(true)

Also note that you have to set them in the channel too.

Pedro Varela
  • 2,296
  • 1
  • 26
  • 32
  • Hi. If I understand correctly, unless settings a "piece of silence" as sound, any notification channel will have sound by default. So, where this `R.raw.silence` comes from? :) – dentex Nov 19 '18 at 12:13
  • 1
    Hi, it is just a silence file placed on raw folder of your project. It is like having your own notification sound go here https://github.com/anars/blank-audio. I think there is a better way now to define a silence notification, read the android documentation again. – Pedro Varela Nov 19 '18 at 16:15