Android Question Notifications, foreground services...

LucaMs

Expert
Licensed User
I have read many times that the best example on how to create a "stable" foreground service is MyLocation, so I tried it.

It works but I am trying to get the same behavoir of some other famous apps, like Whatsapp, Twitter, Zynga Poker, etc.

They are able to receive notifications (obviously from their servers) without there being a first notification fixed on the relative bar, unlike what happens with MyLocation.

I got this by modifying this line:
B4X:
Service.StartForeground(nid, CreateNotification("..."))
this way:
B4X:
Service.StartForeground(0, Null)
This works but then the service will have many problems, it will be destroyed very soon and, when automatically restarted, it will be immediately killed again, until the moment when it does not restart at all.

I'm attaching the slightly modified version of MyLocation:

1) it uses a timer because here, indoors, the gps event never fires;
2) many logs added;
3) the line decribed above.


Is there any way to get the same behavior as those apps?


[I hope you won't recommend FCM to me; also because it will work somehow too, won't it?]
 

Attachments

Last edited:

LucaMs

Expert
Licensed User
These apps use push notifications. The app doesn't need to run in order to receive push notifications.
I don't know this magic 😊

I always "read" two types of "push notifications architecture", here on site: Firebase and your own websocket solution.

The second one uses and needs a service, a "sticky service", at that time. Firebase... I don't know how FCM can send messages to a non-running app, but if it can, we can, or not?
 

kisoft

Active Member
Licensed User
A topic that has been raised repeatedly in the forum. It seems that only great players like FB, WhatsApp, etc., can count on special treatment by phone manufacturers. Their notifications are always active. For us little ones unfortunately there are no such privileges...
 

LucaMs

Expert
Licensed User
A topic that has been raised repeatedly in the forum. It seems that only great players like FB, WhatsApp, etc., can count on special treatment by phone manufacturers. Their notifications are always active. For us little ones unfortunately there are no such privileges...
After searching, I found and maybe understood the question,

I had never searched "very hard" for a list of active services on my smartphone, now I have.

Settings -> System -> Developer options -> Running Services.

I see that few apps have a service running; one of them is... Android, whose active service is called "PushService":
1578996137657.png


Given this, I suppose that apps like Whatsapp use this S.O. service, that Firebase does it too and that for this Erel suggests using Firebase FCM.

Maybe we "ordinary developers" can't access that service, "PushService", in any way ("directly").
 
Last edited:

LucaMs

Expert
Licensed User
I meant push notifications. No settings on your phone will help...
I'm not sure to understand: do you mean that no device settings will help to show notifications when the device is in stand-by?

Whatsapp notifications cause the led to flash, in this case
(instead a test app of mine cannot do it, although in the code I have set:
Notification.Light = True
also
Notification.Sound = True
doesn't work; or, better, I have to manually enable notification sound for my specific app in my device settings :oops:
 

kisoft

Active Member
Licensed User
ANDROID 6 notifications (push) will always appear, regardless of the phone's activity.
ANDROID 9 notifications appear when the phone is active. You won't hear the notification in sleep mode.
This applies to us little ones. I have already written about the great ones.
 

KMatle

Expert
Licensed User
ANDROID 6 notifications (push) will always appear, regardless of the phone's activity.
ANDROID 9 notifications appear when the phone is active. You won't hear the notification in sleep mode.
This applies to us little ones. I have already written about the great ones.
Of course notifications will trigger a sound (here: Android 10). As I always answer: Check your energy settings. A lot of manufacturers have a whitelist (f.e. WhatsApp). Other apps will be stopped when the phone goes to sleep. That's 99% the reason why you "don't receive" fcm messages properly.

On Android 10 energy saving is even more restricted (for every app you can do a lot of settings which can cause issues)

Summary: It's not the app, it's the phone's settings
 

LucaMs

Expert
Licensed User
Of course notifications will trigger a sound (here: Android 10). As I always answer: Check your energy settings. A lot of manufacturers have a whitelist (f.e. WhatsApp). Other apps will be stopped when the phone goes to sleep. That's 99% the reason why you "don't receive" fcm messages properly.

On Android 10 energy saving is even more restricted (for every app you can do a lot of settings which can cause issues)

Summary: It's not the app, it's the phone's settings
Running the project attached to the first post, Erel's MyLocation, on my Android 8 - Huawei , no sounds are played, unless I set this up explicitly and for that app via device settings.
Another project b4a, created today to try FCM, do not need this "user" intervention 😧

[neither flashes the LED (which works, Whatsapp makes it flash)]
 
Last edited:
Top