6

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:

None of the above have worked for me, I would really appreciate if someone could shed some light on this situation.

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • Are you calling notification in startForground(id, notification); function? – hfarhanahmed Feb 16 '19 at 22:56
  • @hfarhanahmed Yes in [LocationUpdatesService.java](https://github.com/googlesamples/android-play-location/blob/master/LocationUpdatesForegroundService/app/src/main/java/com/google/android/gms/location/sample/locationupdatesforegroundservice/LocationUpdatesService.java#L226) at line 226 – Doğukan Özdemir Feb 16 '19 at 23:06
  • https://stackoverflow.com/questions/39809702/how-to-show-a-notification-without-a-sound-java check this solution – hfarhanahmed Feb 16 '19 at 23:15
  • the code I use doesn't even have `builder.setDefaults(Notification.DEFAULT_ALL);` – Doğukan Özdemir Feb 16 '19 at 23:17
  • add .setDefaults(Notification.DEFAULT_ALL); after line 298 in LocationUpdatesService.java – hfarhanahmed Feb 16 '19 at 23:20
  • @hfarhanahmed Nope, still makes a notification sound. – Doğukan Özdemir Feb 16 '19 at 23:24
  • 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 via nm.deleteNotificationChannel(nChannel.getId()); and recreate it with nm.createNotificationChannel(nChannel); via https://stackoverflow.com/questions/45919392/disable-sound-from-notificationchannel/45920861 – hfarhanahmed Feb 16 '19 at 23:28
  • @hfarhanahmed I have already tried that as I stated in my question. – Doğukan Özdemir Feb 16 '19 at 23:31
  • did you changed the channel name as well?? – hfarhanahmed Feb 16 '19 at 23:32
  • @hfarhanahmed I didn't create any new channels I just set the importance to `IMPORTANCE_LOW` when I was creating the INITIAL channel – Doğukan Özdemir Feb 16 '19 at 23:34
  • try creating a different channel, I thing you are doing everything correct. – hfarhanahmed Feb 16 '19 at 23:36
  • @hfarhanahmed This solved my issue please post it as an answer so I can accept it! – Doğukan Özdemir Mar 02 '19 at 20:44
  • Possible duplicate of [Disable sound from NotificationChannel](https://stackoverflow.com/questions/45919392/disable-sound-from-notificationchannel) – tir38 Mar 28 '19 at 02:05
  • Does this answer your question? [How to show a notification without a sound java](https://stackoverflow.com/questions/39809702/how-to-show-a-notification-without-a-sound-java) – Marvin Apr 21 '23 at 15:14

5 Answers5

4

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

hfarhanahmed
  • 184
  • 1
  • 7
3

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);
AnswerZhao
  • 366
  • 3
  • 13
1

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.

Don Ha
  • 689
  • 5
  • 9
1

I might be a little late with answer, but did you try reinstalling the app? It's simple and it may fix problem.

0

Maybe I am late to answer this question but for other seaker for an answer, i will add my answer:

NotificationCompat.Builder notificationBuilder = new 
                      NotificationCompat.Builder(this, CHANNEL_ID)
.setPriority(NotificationCompat.PRIORITY_LOW)
   .setSilent(true);