Wagner Tiburcio
Wagner Tiburcio

Reputation: 153

Firebase Navigator is not working on onMessageOpenedApp

I read all topics on stackoverflow but none of them worked for me. I'm trying to use navigator when you click on the notification. Everything is working fine but when you click on it there's no error and no navigating, the notification just disappears. What can i do? I will share the code

 initState(){
    super.initState();
  var initializationSettingsAndroid = AndroidInitializationSettings('@mipmap/ic_launcher');
  var initializationSettings = InitializationSettings(android: initializationSettingsAndroid,);
  flutterLocalNotificationsPlugin.initialize(initializationSettings);
    FirebaseMessaging.onMessage.listen((event) {
      LocalNotificationService.display(event);
    });
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
                              Get.to(Conversa()); // I've tried to use navigator too, it not worked
                          });
  }

OBS: If i try to use navigator inside onMessage it works but i don't want to automatically send the user to the x page

Upvotes: 1

Views: 5880

Answers (2)

Bauroziq
Bauroziq

Reputation: 1231

For foreground & background you can set the payload in flutter_local_notification from onMessage.

FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      RemoteNotification? notification = message.notification;

      if (notification != null && !kIsWeb) {
        String? payload = message.data;

        flutterLocalNotificationsPlugin.show(
            ...
            payload: payload);
      }
});

Then, use payload value to manage your navigate in onSelectNotification

await flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: (String? payload) async {
     // Navigate
});

Upvotes: 0

user18309290
user18309290

Reputation: 8370

Use onMessageOpenedApp only when the application is in the background, not foreground or terminated.

A Stream event will be sent if the app has opened from a background state (not terminated).

See onMessageOpenedApp for details.

Upvotes: 2

Related Questions