Flutter - FirebaseMessaging.onMessageOpenedApp.listen is not triggered
Solution 1
For the first question:
To view the logs from your app, you can use the 'Logcat' tab in Android Studio or IntelliJ:
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
Kristi Jorgji
Updated on December 26, 2022Comments
-
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
(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
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 over 2 yearsdo 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 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 almost 3 yearsThanks, 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 almost 3 yearsRight 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 almost 3 yearsYes, one goes inside
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message)
-
epynic almost 3 years
_firebaseMessagingBackgroundHandler
is your top level function as in example. -
Kristi Jorgji almost 3 yearsI did this and still did not work..., before running app in main.dart I registered
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
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 almost 3 yearsI 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 almost 3 yearsNo 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 almost 3 yearscan you once try upgrading to the firebase messaging firebase core package and try