How to Open Particular Screen on Clicking on Push Notification for Flutter

Navigate to another screen when I click on Firebase push notification either in foreground or background in Flutter

In your code, you are using the flutter_local_notifications plugin and you are creating local notification when you get a push notification from firebase messaging.

Since the notification is not created by firebase messaging so, you are not getting on tap callback in getInitialMessage and onMessageOpenedApp.

In order to get on tap callback on local notifications, you can pass a callback function while initializing flutter_local_notifications

flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: onSelectNotification);

void selectNotification(String payload) async {
if (payload != null) {
debugPrint('notification payload: $payload');
// Here you can check notification payload and redirect user to the respective screen
await Navigator.push(
context,
MaterialPageRoute<void>(builder: (context) => SecondScreen(payload)),
);
}
}

For more, you can check flutter_local_notifications documentation

Flutter notification open dialog when app is killed

I refer some answers from other threads and resolved it by using this getInitialMessage()

FirebaseMessaging.instance.getInitialMessage().then((message) {
if (message != null) {
RemoteNotification? notification = message.notification;
AndroidNotification? android = message.notification?.android;
if (notification != null && android != null) {
// TODO
// Do any logic, for example dialog
}
}
});

References:
https://stackoverflow.com/a/72497605/15075643
https://stackoverflow.com/a/68517166/15075643



Related Topics



Leave a reply



Submit