Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - FirebaseMessaging.onMessageOpenedApp.listen is not triggered

I am using:

flutter version 2.2
firebase_messaging: ^10.0.2

I receive push notification, then click on it and the app is opened. Then I do not see FirebaseMessaging.onMessageOpenedApp.listen getting called (the callback is sending debug email to me, but I don't receive any)

My questions are

  1. (optional) How can I debug android app with android studio debugur simulating case above, so app is killed not opened, then is opened via notification

  2. What can be the issue here ? Why that stream is not triggered ? I initialise it in main.dart

PS: All other methods work fine, so if app is on foreground, onMessage.listen works great. I need to handle onMessageOpenedApp so I can redirect user to proper view based on notification information

like image 205
Kristi Jorgji Avatar asked Feb 21 '26 19:02

Kristi Jorgji


2 Answers

For the first question:

To view the logs from your app, you can use the 'Logcat' tab in Android Studio or IntelliJ: enter image description here

For the second question:

If your app is terminated and you want to receive a notification click callback, you should use:

FirebaseMessaging.instance.getInitialMessage().then((message) {
  if (message != null) {
    // DO YOUR THING HERE
  }
});

, because according to the Flutter team's comments for onMessageopenedApp function:

 /// If your app is opened via a notification whilst the app is terminated,
  /// see [getInitialMessage].
like image 68
Rusu Dinu Avatar answered Feb 23 '26 11:02

Rusu Dinu


I've been in this issue for a while and just noticed that, there are 3 workarounds provided by firebase_messaging plugin.

  1. initial message
  2. onMessage
  3. onMessageOpenedApp

You can find details below:

  1. when app is in opened state onMessage callback will be called which is actually a Stream<RemoteMessage> and there will be no notification in notification tray. so you can handle it like
FirebaseMessaging.onMessage.listen((message) {
      // handle accordingly
    });
  1. when app is in pause state i.e. app is active in background, a notification will be appeared in notification tray and on click of that notification onMessageOpenedApp callback will be called which is also a Stream<RemoteMessage> so you can handle it like
FirebaseMessaging.onMessageOpenedApp.listen((message) {
      // handle accordingly
    });
  1. third case is when app is in terminated or killed state. in that case notification will be shown in notification state but as your app is not in active state so that Stream<RemoteMessage> cannot be triggered. So in that case firebase_messaging provides you getInitialMessage callback which is actually a Future<RemoteMessage?>. you can use it like:
FirebaseMessaging.instance.getInitialMessage().then((message) {
      if (message != null) {
        // handle accordingly
      }
    });

Note that getInitializeMessage callback will be triggered every time user opens the app if its handled in main.dart file. so maybe message is null, to prevent null pointer exception I put if(message != null) condition here

like image 21
Muhammad Usama Ijaz Avatar answered Feb 23 '26 11:02

Muhammad Usama Ijaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!