Flutter - FirebaseMessaging.onMessageOpenedApp.listen is not triggered

4,705

Solution 1

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

Solution 2

To view logs when app is killed in your case, have you tried flutter logs command, just plug in your device in usb debugging and run flutter logs in you terminal all your print messages will show up here.

with regard to FirebaseMessaging.onMessageOpenedApp.listen getting called when app is launched is because you need to define the background messaging handler

/// Define a top-level named handler which background/terminated messages will
/// call.
///
/// To verify things are working, check out the native platform logs.
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  // If you're going to use other Firebase services in the background, such as Firestore,
  // make sure you call `initializeApp` before using other Firebase services.
  await Firebase.initializeApp();
  print('Handling a background message ${message.messageId}');
}

check the complete firebase_messaging example here main.dart

Share:
4,705
Kristi Jorgji
Author by

Kristi Jorgji

Updated on December 26, 2022

Comments

  • Kristi Jorgji
    Kristi Jorgji over 1 year

    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

    • Alexa289
      Alexa289 over 2 years
      do you find the solution? do you find a workaround for this? I have exactly the same issue , unfortunately your github issue you create is also has no answer and has been limited github.com/FirebaseExtended/flutterfire/issues/6483
    • Mark
      Mark over 2 years
      @Kristi Jorgji Any luck with a solution to this? Ive been working on solving this for 3 days straight with no luck. There can't be a bug this big in the framework, so we must be doing something wrong?
  • Kristi Jorgji
    Kristi Jorgji almost 3 years
    Thanks, I have registered already the FirebaseMessaging.onMessageOpenedApp but after I do one api call to fetch app config ? can that be the case that is being called late (before upgrade of firebase_messaging in old package worked fine like this), but maybe now is missing some event...
  • Kristi Jorgji
    Kristi Jorgji almost 3 years
    Right now I initialise firebase right away! on main.dart then later on after one api call do FirebaseMessaging.onBackgroundMessage((message) async { _logger.d('onBackgroundMessage: ${message.data}'); _handle(message); });. So should I initialise twice Firebase.initializeApp() like in the provided example, where second time is inside _firebaseMessagingBackgroundHandler
  • epynic
    epynic almost 3 years
    Yes, one goes inside Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message)
  • epynic
    epynic almost 3 years
    _firebaseMessagingBackgroundHandler is your top level function as in example.
  • Kristi Jorgji
    Kristi Jorgji almost 3 years
    I did this and still did not work..., before running app in main.dart I registered FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBack‌​groundHandler); and function is top level looks like /// Define a top-level named handler which background/terminated messages will /// call. /// Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async { await Firebase.initializeApp(); getLogger('app_runner').d('Firebase.onBackgroundMessage: ${message.data}'); PushNotificationsManager.handle(message); }
  • Kristi Jorgji
    Kristi Jorgji almost 3 years
    I moved it as first damn line in main.dart and now works.... i can see in logs getting called but have to sort out synchronisation with rest of app now, because i want to handle the notification after api call is done to see if user logged in etc then decide where to redirect them in which view based on notitifation type
  • Kristi Jorgji
    Kristi Jorgji almost 3 years
    No this is still not triggered!!! onMessageOpenedApp is not called, just onbackgroundmessage is called when app is closed... I want to handle action when user clicks on notitiation then app is opened (this worked great before upgrade new version)
  • epynic
    epynic almost 3 years
    can you once try upgrading to the firebase messaging firebase core package and try