skip to Main Content

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.

4

Answers


  1. 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
    
    
    
    }
    
    Login or Signup to reply.
  2. 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);
          }
        });
    
    Login or Signup to reply.
  3. I hope it will be helpful.
    First in method to show push notification add reader of a payload.

    void showFlutterNotification(RemoteMessage message) {
      RemoteNotification? notification = message.notification;
      AndroidNotification? android = message.notification?.android;
      if (notification != null && android != null && !kIsWeb) {
        flutterLocalNotificationsPlugin.show(
            notification.hashCode,
            notification.title,
            notification.body,
            NotificationDetails(
              android: AndroidNotificationDetails(
                channel.id,
                channel.name,
                channelDescription: channel.description,
                // TODO add a proper drawable resource to android, for now using
                // one that already exists in example app.
                icon: 'launch_background',
              ),
            ),
            // Add reading of a payload based on the contents of notification
            // In my case:
            payload: notification.data['params']);
      }
    }
    

    Then somewhere in the application for example in initState method try to call the payload.

    NotificationAppLaunchDetails? notificationAppLaunchDetails = await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
        if (notificationAppLaunchDetails != null && notificationAppLaunchDetails.didNotificationLaunchApp) {
          var payload = notificationAppLaunchDetails.notificationResponse?.payload;
          if (payload != null && payload.isNotEmpty) {
            // Your code to handle the payload for routing.
          }
        }
    

    Be aware that flutterLocalNotificationsPlugin is properly initialized in main().

    Login or Signup to reply.
  4. onMessageOpenedApp this will work only once your app is in background state (not terminated)

    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
      log('onMessageOpenedApp');
      //onTap navigation app is in background not terminated 
    });
    

    For foreground state you can use local notification plugin like this

     FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
          print('onMessage');
          showFlutterNotification(message);
        });
    
        final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
           const AndroidInitializationSettings initializationSettingsAndroid =
                AndroidInitializationSettings('ic_launcher');
            const InitializationSettings initializationSettings =
                InitializationSettings(
              android: initializationSettingsAndroid,
            );
            await flutterLocalNotificationsPlugin.initialize(
              initializationSettings,
              onDidReceiveNotificationResponse:
                  (NotificationResponse notificationResponse) {
                if (notificationResponse.notificationResponseType ==
                    NotificationResponseType.selectedNotification) {
                   //onTap navigation app is in foreground state
                  // payload 
                  var payload = notificationResponse.payload;
                }
              },
              onDidReceiveBackgroundNotificationResponse:
              null,
            );
    

    Pass payload data while showing notification with local notification plugin example below

      void showFlutterNotification(RemoteMessage message) {
        RemoteNotification? notification = message.notification;
        AndroidNotification? android = message.notification?.android;
        if (notification != null && android != null) {
          flutterLocalNotificationsPlugin.show(
            notification.hashCode,
            notification.title,
            notification.body,
            NotificationDetails(
              android: AndroidNotificationDetails(
                channel.id,
                channel.name,
                channelDescription: channel.description,
                icon: 'ic_launcher',
              ),
            ),
            ///here passing payload
            payload: jsonEncode(message.data),
          );
        }
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search