Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foreground Service with a Silent Notification

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:

  • in LocationUpdatesService.java at added159 tried mChannel.setSound(null,null);
  • in LocationUpdatesService.java at line 296 changed .setPriority(Notification.PRIORITY_HIGH) to .setPriority(Notification.PRIORITY_LOW)
  • in LocationUpdatesService.java at line 158changed NotificationManager.IMPORTANCE_DEFAULT to NotificationManager.IMPORTANCE_LOW)
  • in LocationUpdatesService.java at line 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.

like image 734
Doğukan Özdemir Avatar asked Feb 16 '19 22:02

Doğukan Özdemir


4 Answers

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

like image 110
hfarhanahmed Avatar answered Oct 13 '22 23:10

hfarhanahmed


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);
like image 21
AnswerZhao Avatar answered Oct 13 '22 22:10

AnswerZhao


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.

like image 26
Don Ha Avatar answered Oct 13 '22 21:10

Don Ha


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

like image 35
Rastko Stamenkovic Avatar answered Oct 13 '22 23:10

Rastko Stamenkovic