Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

This is usually done by sending a push message with no content, that just prompts the app to fetch messages whenever new ones are available. Of course, if the App then proceeds to post the message contents to a notification in plaintext, well then... But you can adjust the settings for what to display in the notif and of course just disable the notifications, so no other app will be able to listen.


For that the app must be in foreground. How does WhatsApp manage to be always on the foreground? My app always gets killed by Android.


On Android, push notifications are handled by a service defined in your application's manifest. The system launches the service when a notification is received, and calls its onMessageReceived() function which you customize to display the notification's layout, add actions to tap events, etc.

https://firebase.google.com/docs/cloud-messaging/android/rec...


Apparantly `onMessageReceived()` is called only when the app is in foreground.


I spent a day of my life at work over this annoying confusion. https://firebase.google.com/docs/cloud-messaging/android/rec...

It is counterintuitive, feels arbitrary, and logical thinking will cause you to misread the chart they provide. Whether that method is called is based on the payload of the notification as well as making sure you've properly registered the types of notifications. This is helpfully spread between Android docs and the Firebase docs. The exact differences between the notifications that will trigger this (while still being compatible with iOS) is left as an exercise to the reader.


It's not that bad if you completely ignore Firebase and override onHandleIntent. Now you can choose what to do with the message without caring about the difference between "data" and "notification" messages or whether your app is in the foreground.


Can you elaborate on this a bit? Is it possible to use a non-Firebase push notification service on Android, that properly works in the background? Our the app still needs to use FCM but you can override the client-side handler somehow?


Both are possible. The source code to the Firebase push messaging service is available on Maven and GitHub, so you can re-implement it. Otherwise, you subclass the same service but override `onHandleIntent` instead of `onMessageReceived`, and avoid the code path that immediately displays the notification for 'notification' messages when the app is in the background.


by having a permanent/persistent notification. for example KeePass2Android does it pretty well. (usually when the battery saver kicks in it kills these semi-foreground things.)

when the phone starts the user has to first start the app though (and I found this to be true for Signal, Skype and probably for WhatsApp too)


WhatsApp doesn't have a persistent notification though.

> when the phone starts the user has to first start the app though

Just tested this and this is not the case with WhatsApp. I am on a Samsung and I am not sure if WhatsApp get's any special treatment.


ah, I probably misunderstood the context. KeePass2Android does this to prevent Android from putting it to "sleep", which leads to the in-memory unlocked DB getting unloaded.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: