Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silent notifications in foreground services

I have multiple apps that kind of work together to do the same job and they all belong to the same developer. Each app runs a long-running service in the background and keeps processing user's input. The problem is that those services cannot run in the background for a long time because Android system will kill them. So I want to use foreground services instead of background ones to prevent the system from killing them. However, I don't want to annoy the users with multiple different notifications in the notification drawer.

I found out that creating a notification without assigning a channel in Android O, will let the system start the foreground service without showing a notification. Something like the following:

Notification.Builder builder = new Notification.Builder(context);
builder.setContentTitle(......);
builder.setTicker(......);
builder.setContentText(......);
builder.setSmallIcon(......);
builder.setLargeIcon(......);
Notification notification = builder.build();
startForeground(111, notification);

So I was thinking of showing a notification by creating a notification with a channel from one app and create a notification without a channel for the other apps as I described earlier. In that case, the user will see one notification for all my apps.

That solution works well for me. But I am wondering if it is an unintended use of the notification in the foreground services. I am afraid that Google will suspend my apps for doing that!!

Do you guys know if it is okay to implement that solution? Or is there any way to stack the notifications together in a group even though they are different apps?

My goal is just to make the notification less annoying to the user. Also, I am aware of JobScheduler & JobIntentService solutions. But they don't do the job in my case because I want to keep the service running. Not like do one job and stop the service...

like image 330
DigitalPerson Avatar asked Nov 17 '22 17:11

DigitalPerson


1 Answers

You can create notification channel with IMPORTANCE_LOW (https://developer.android.com/training/notify-user/channels#importance). There shouldn't be sound.

Or you can also use setOnlyAlertOnce() (https://developer.android.com/training/notify-user/build-notification#Updating) and the sound will be only once.

like image 119
Tomasz A Avatar answered Jan 16 '23 17:01

Tomasz A