Android Question [SOLVED] How to update the foreground notification at any time?

Sandman

Expert
Licensed User
Longtime User
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
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.
 

Sandman

Expert
Licensed User
Longtime User
There is no reason to show two notifications.

Thank you, that gives me hope there is a solution. :)


What is the purpose of the first notification?

Think of the app as a timetracker. (There's also a GPS thing going now and then, and some communication with a server on the net.)

These are the two main messages in the notification (that is always visible while using the app):

”You are currently not tracking your time”
and ”You are tracking time for project X”

(I'm going to try to show my own notification before starting the service to see if that makes a difference...)
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
(I'm going to try to show my own notification before starting the service to see if that makes a difference...)

No, that didn't make a difference at all.

If I create and show my own notification first, and store the notification in a variable, I still get a second notification if I do this in the service:
B4X:
Service.AutomaticForegroundNotification = GLOBALS.myOwnNotification

If I skip that part entirely I get a generic notification with a square as icon, and the app name as text.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Assuming that it takes a few seconds for the service to communicate with the server then you should consider just showing the two notifications until the communication completes and then call StopAutomaticForeground.

Other options:
Set AutomaticForegroundMode to Never (in Service_Create).
In service_Start you should remove the non-related foreground notification and show it again with Service.StartForeground.
Once communication completes call Service.StopForeground and show the regular notification again.
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
I see. So option 2 is basically a swap between the service notification, and the real one, each one looking the same. I think that might be the way to go here, I'm not really tempted to show a second notification. (It can take up to a minute to finish.)

I would have thought it would be possible to point the service notification in the direction of the already visible one, to stop a second one from showing. But that doesn't really seem to be an option, correct? Is this a limitation of B4A or Android? (It seems like a bad situation for me, I already have a notification presence, and all this does for me is making the code more complicated for no added value.)
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
Just a follow-up for people finding this thread in the future: Swapping between my "real" notification and the one forced by the service worked nicely.

When I swap to the service notification I can actually reuse the already visible one, so that's rock solid without any visual indication that anything happened.

When I swap from the service notification to the real one, I must cancel the service one and then .Notify the real one. And there we have a split second where nothing is displayed. Looks a tiny little bit strange, but not so much that the typical user should notice it. I can live with that.
 
Upvote 0
Top