Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug(messaging): notificationReceived fires only for the last message received while the app is killed #210

Closed
crowbait opened this issue Sep 30, 2022 · 12 comments
Assignees
Labels
bug/fix Something isn't working package: messaging platform: android Android platform platform: ios iOS platform
Milestone

Comments

@crowbait
Copy link

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! 👌
@robingenz
Copy link
Member

Thank you for your request. I will have a look. Should be a bug.

@robingenz
Copy link
Member

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 ionic-team/capacitor#5967)
As soon as Capacitor supports it, I will fix it on Android and iOS.

@robingenz robingenz added platform: android Android platform bug/fix Something isn't working platform: ios iOS platform and removed needs: triage labels Oct 1, 2022
@robingenz robingenz self-assigned this Oct 1, 2022
@crowbait
Copy link
Author

crowbait commented Oct 1, 2022

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?

@robingenz
Copy link
Member

robingenz commented Oct 1, 2022

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).

@crowbait
Copy link
Author

crowbait commented Oct 1, 2022

Is there any way of achieving exactly that? Handling notifications while the app has been killed?

@robingenz
Copy link
Member

Yes, with native code (e.g. Capacitor Plugin).
What is your specific use case?

@crowbait
Copy link
Author

crowbait commented Oct 1, 2022

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).

@robingenz
Copy link
Member

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.

@crowbait
Copy link
Author

crowbait commented Oct 1, 2022

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.

@crowbait
Copy link
Author

crowbait commented Oct 2, 2022

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.

@crowbait
Copy link
Author

crowbait commented Oct 2, 2022

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: ionic-team/capacitor-plugins#1211 (comment)

@robingenz robingenz changed the title bug: (messaging) notificationReceived-listener doesn't fire if app is killed bug: (messaging) notificationReceived fires only for the last message received while the app is killed Oct 4, 2022
@robingenz robingenz changed the title bug: (messaging) notificationReceived fires only for the last message received while the app is killed bug(messaging): notificationReceived fires only for the last message received while the app is killed Feb 11, 2023
@robingenz robingenz added this to the v2.0.0 milestone Mar 31, 2023
@robingenz
Copy link
Member

This should be fixed in Capacitor 5 (see ionic-team/capacitor#5967 and #355).

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug/fix Something isn't working package: messaging platform: android Android platform platform: ios iOS platform
Projects
None yet
Development

No branches or pull requests

2 participants