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...