1

I'm implementing code for notification navigation after user press on notification. Below is my code.

FirebaseMessaging.instance
      .getInitialMessage()
      .then((RemoteMessage? message) {
    if (message != null) {
      handleNavigationToScreen(message);
    }
  });

  FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
    handleNavigationToScreen(message);
  });

Code is working when application is killed or in background. The problem that i have is when application is in foreground and receive notification.

In this case i handle notification with:

FirebaseMessaging.onMessage.listen(showFlutterNotification);
void showFlutterNotification(RemoteMessage message) async {
 flutterLocalNotificationsPlugin.show(
      notification.hashCode,
      notification.title,
      notification.body,
      NotificationDetails(
        android: AndroidNotificationDetails(
          channel.id,
          channel.name,
          channelDescription: channel.description,
          styleInformation: bigPictureStyleInformation,
          icon: '@mipmap/ic_launcher',
        ),
      ),
    );
}

The problem is when i press on notification that was shown with "flutterLocalNotificationsPlugin.show", is not trigged FirebaseMessaging.onMessageOpenedApp.listen.

Is there any other way to get callback on notification press to handle screen navigation.

Thanks

1 Answer 1

0

Before showing notifications in the foreground, you need to call the following methods :

await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
configLocalNotification();
static void configLocalNotification() {
  var initializationSettingsAndroid = const AndroidInitializationSettings("@mipmap/ic_launcher");
  var initializationSettingsIOS = const DarwinInitializationSettings();

  var initializationSettings = InitializationSettings(
    android: initializationSettingsAndroid,
    iOS: initializationSettingsIOS,
  );

  flutterLocalNotificationsPlugin.initialize(
    initializationSettings,
    onDidReceiveNotificationResponse: (NotificationResponse notificationResponse) {
      onSelectNotification(notificationResponse.payload ?? '');
    }
  );
}
static onSelectNotification(String payload) async {
  var message = jsonDecode(payload);
  var remoteMessage = RemoteMessage.fromMap({'data': message});
  handleNavigationToScreen(remoteMessage);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.