Context:
I have been using Google's LocationUpdatesForegroundService example project to learn a bit more about services.
I have downloaded the project via Github desktop and ran it on my phone, everything is great and the project does what it's intended to do.
My phone is Android 8.0.0, API 26
Problem:
The foreground service notification shows up on the status bar once the app is terminated, as soon as that happens I hear a notification sound(default sound). However, I would like the notification to be silent, like in some location-based apps(eg: Life360)
What I've tried so far:
159
tried mChannel.setSound(null,null);
296
changed .setPriority(Notification.PRIORITY_HIGH)
to
.setPriority(Notification.PRIORITY_LOW)
158
changed NotificationManager.IMPORTANCE_DEFAULT
to NotificationManager.IMPORTANCE_LOW)
300
added setSound(null)
None of the above have worked for me, I would really appreciate if someone could shed some light on this situation.
The solution is to use NotificationManager.IMPORTANCE_LOW and create a new channel for it. Once a channel is created, you can't change the importance (well, you can, but the new importance is ignored). The channel information appears to get stored permanently by the system and any channel created is only deleted when you uninstall the app. you can delete the channel as
nm.deleteNotificationChannel(nChannel.getId());
and recreate it with
nm.createNotificationChannel(nChannel);
via stack overflow answer here Disable sound from NotificationChannel
In the LocationUpdatesForegroundService, when the service is onUnbind, it will invoke 'startForeground(NOTIFICATION_ID, getNotification())', then the Notification will show with sound. So you need to change the NotificationChannel follow the below code
mChannel.setSound(null, null);
mChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
mChannel.setSound(null,null) will do it, but you need to uninstall/reinstall the app for it to take effect. Or changing CHANNEL_ID to a different value will also recreate channel with updated setting.
I might be a little late with answer, but did you try reinstalling the app? It's simple and it may fix problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With