Home > Mobile >  FCM FirebaseMessaging.onMessageOpenedApp Won't work
FCM FirebaseMessaging.onMessageOpenedApp Won't work

Time:09-21

Although all the other functions work perfectly, I receive the message and display it in all conditions, in the background, foreground or initial message.

in the background:

Future<void> backgroundHandler(RemoteMessage message) async {
  print(message.data.toString());
  print(message.notification!.title);
}

FirebaseMessaging.onBackgroundMessage(backgroundHandler);

in the foreground:

FirebaseMessaging.onMessage.listen((message) {
      if (message.notification != null) {
        add(NotificationRecieved(message));
      }
    });

in the initialization of the application:

await FirebaseMessaging.instance.getInitialMessage().then((message) {
      if (message != null) {
        LocalNotificationService.display(message);
        return message.data["route"];
      }
    });

but this doesn't work: when I click on the notification

FirebaseMessaging.onMessageOpenedApp.listen(
      (message) {
        toast(message.data.toString());
        var route = message.data["route"];
        Navigator.pushNamed(context, route);
      },
    );

the function (onMessageOpenedApp.listen) itself isn't being called.

CodePudding user response:

Do Checking Like This:


    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
     
      RemoteNotification? notification = message.notification;
      AndroidNotification? android = message.notification!.android;
   
      if (notification != null && android != null) {


-----do your action here



}

CodePudding user response:

Try this:

FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage? message) {
      if (message != null && message.data["route"] != null) {
        toast(message.data.toString());
        var route = message.data["route"];
        Navigator.pushNamed(context, route);
      }
    });
  • Related