bug: (messaging) `notificationReceived` fires only for the last message received while the app is killed
Plugin(s):
"@capacitor-firebase/messaging": "^1.1.0"
Platform(s):
Android
Current behavior:
If a data-only notification is received while the app is killed or the device is in doze, the notficationRecieved listener event does not fire. When the app is opened the next time, the listener fires only for the last message recieved while the app was killed. Every other message is dropped.
Expected behavior:
Code given to the listener should be executed regardless of doze or killed app. Documentation states If the app is in the background, then this listener is only called on data push notifications., but does not make any statement about the app being killed.
Steps to reproduce:
Add listener and send data-only notification to device while app is killed:
FirebaseMessaging.addListener('notificationReceived', event => {
console.log("Notification recieved: ", event); //fires if message is recieved while app in foreground or background
if(event.notification.data?.payload){ //but not if app is killed
//do something traceable which doesn't need console
//for example, write a small file with messageid as filename
}
});
message:
token: "notificationtoken",
data: {
payload: "messagePayload"
},
android: {
priority: "high"
}
Capacitor doctor:
Capacitor Doctor
Latest Dependencies:
@capacitor/cli: 4.3.0
@capacitor/core: 4.3.0
@capacitor/android: 4.3.0
@capacitor/ios: 4.3.0
Installed Dependencies:
@capacitor/cli: 4.1.0
@capacitor/core: 4.2.0
@capacitor/ios: 4.2.0
@capacitor/android: 4.2.0
[success] Android looking great! 👌
Thank you for your request. I will have a look. Should be a bug.
I looked at it and was able to reproduce the problem. The problem also exists on iOS. However, I cannot currently fix the bug on iOS because Capacitor does not allow it. I have created a feature request. (see https://github.com/ionic-team/capacitor/issues/5967) As soon as Capacitor supports it, I will fix it on Android and iOS.
From the issue you opened, I read that immediate handling of data-only notifications (eg displaying a local notification in response) isn't even the intended behavior? So the fact that (although just one) notifications are handled only when the app launches the next time is intended?
Immediate handling of data-only notifications is the intended behavior, but only if the application is running in the background (so only if the application was not force closed).
Is there any way of achieving exactly that? Handling notifications while the app has been killed?
Yes, with native code (e.g. Capacitor Plugin). What is your specific use case?
Chat feature in the app should display notifications as soon as they come (minus delay and restrictions, but before the user manually opens the app). As user could still just dismiss the notification, I need to perform operations on it before the user can dismiss it - so normal message notifications are unfeasible and data notifications don't work from within JS. So guess that means I have to learn Java after all. I just hope I can kind of follow a guide on how to implement FCM handling on Android without the native capacitor plugin breaking everything (I still need some functionality from JS to handle notifications when the app is open after all).
You could send two notifications (one Notification message and one Data message) at the same time. This is how a client of mine does it with a similar problem. Maybe this would also be a solution for you.
That is a nice idea. However, it would mean double the calls to firebase cloud functions (if I had a server that could handle a call on every message, I'd just save them to the DB and fetch them on login)... So yes, I think I will have to investigate the Java side of things in the coming days.
Just for the sake of being thorough: I just tested the behavior in a production (non-debug) app via internal testing track on google play.
- messages before the most recent one still get dropped when the app opens
- data-messages don't get handled if the app has been swiped ("closed") from recent apps tray; should be passed to listener on next app launch then
I did this test because I learned that push notifications allegedly have different behavior in debug vs production and also Android is said to kill services associated with an app on close if the app is debug.
This might be a bit off-topic, but for anyone passing here on the hunt for a solution to my problem, adding your own FirebaseMessagingService extender doesn't play well with capacitor either. I've written about that here: https://github.com/ionic-team/capacitor-plugins/issues/1211#issue-1393872221
This should be fixed in Capacitor 5 (see https://github.com/ionic-team/capacitor/issues/5967 and #355).