My app always has a notification open, to show user status. Periodically a service, svcDoStuff, is run to communicate with a server online. Since Android 8 the periodic service needs to be a foreground service, to ensure it won't get killed while doing it's thing. When the foreground service is running, a second notification is displayed, which makes sense for most apps to make it absolutely clear to end user that something is happening.
Buuut... I already have a notification. So it's just weird to have a second one forced upon me. I understand that there isn't a whole lot I can do about the foreground notification, but that doesn't mean I think it's ok to have two notifications.
So I've been thinking what the best strategy is. Should I simply make the service AUTOMATIC_FOREGROUND_ALWAYS so that the notification for the service is always visible? And then use that notification for my ordinary app notifications?
However, when using the service notification, I don't get to provide an ID. This means I can't easily replace the contents of that notification in my ordinary notification class. And I can't set the notification using
As far as I'm able to tell, I'm forced to set the notification from within the service with a sub like this:
And this is the code in my notification class:
But that doesn't change the notification, obviously - as one can only set the notification in Service_Create.
I'm a bit lost here, all help is appreciated on how to solve this so I only have one notification, and so that I can actually update it as I see fit.
Buuut... I already have a notification. So it's just weird to have a second one forced upon me. I understand that there isn't a whole lot I can do about the foreground notification, but that doesn't mean I think it's ok to have two notifications.
So I've been thinking what the best strategy is. Should I simply make the service AUTOMATIC_FOREGROUND_ALWAYS so that the notification for the service is always visible? And then use that notification for my ordinary app notifications?
However, when using the service notification, I don't get to provide an ID. This means I can't easily replace the contents of that notification in my ordinary notification class. And I can't set the notification using
B4X:
svcDoStuff.AutomaticForegroundNotification = theNewNotification
As far as I'm able to tell, I'm forced to set the notification from within the service with a sub like this:
B4X:
Public Sub setNotification (n As Notification)
Service.AutomaticForegroundNotification = n
End Sub
And this is the code in my notification class:
B4X:
Dim n As NB6
n.Initialize("status", "Status", "LOW").SmallIcon(icon).Visibility("PUBLIC")
lastNotification = n.Build(notificationBody, "", "", Main)
CallSub2(svcDoStuff, "setNotification", lastNotification)
But that doesn't change the notification, obviously - as one can only set the notification in Service_Create.
I'm a bit lost here, all help is appreciated on how to solve this so I only have one notification, and so that I can actually update it as I see fit.