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
How to Disable Cache in Android Webview
Architecture Components: Observer Keep Observing Even After Removing It on Ondestroy
Access Denied Finding Property "Camera.Hal1.Packagelist"
Failure Install Parse Failed Manifest Malformed
Add Text to Image in Android Programmatically
How to Know When My App Has Been Killed
Android Application Icon Not Showing Up
How to Refresh the Fragment After Activity Finished in Android
How to Select Only One Checkbox in Recyclerview and Notifydataset Changed
Android - Show Button After Swiping Recyclerview Item
How to Handle Parameters That Can Be an Array or Object in Retrofit on Android
Retrofit:Getting Error ** End of Input At Line 1 Column 1**
Failed to Resolve: Android.Support.Design:28.0.0-Rc02
Notify Observer When Item Is Added to List of Livedata
How to Retry Retrofit Call on Http Errors (401) When Using Rxjava
Dynamically Setting Visibility of Textview in Recyclerview