Reputation: 113
I am using version 9.0 of firebase messaging and wishing the user to a specific screen when receiving and clicking on a notification when the app is closed.
I can currently manage with the app open or minimized, but I don't understand how to perform this action when the app is not open.
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
Model model = Model();
id = "sasassa;
Get.offNamed("$SCREEN_ROUTE",arguments: model);
Storage storage = Storage();
storage.setReceivedNotification(true);
print("teste notificação recebida");
}
FirebaseMessaging.instance.getInitialMessage().then((message){
if (message != null){
Model model = Model();
id = "sasassa;
Get.offNamed("$SCREEN_ROUTE",arguments: model);
}
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print("a");
storage.setReceivedNotification(true);
Model model = Model();
id = "sasassa;
Get.offNamed("$SCREEN_ROUTE",arguments: model);
//Navigator.pushNamed(context, '/events');
});
FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
print("b");
storage.setReceivedNotification(true);
print("teste b $message");
String? corpo = message.notification?.body;
String? titulo = message.notification?.title;
String? image = message.notification?.android?.imageUrl;
//showMyDialog(titulo: titulo,corpo: corpo);
print("$corpo $titulo $image");
});
I tried to use shared preference in backgroundHandler, but this way the redirect will happen even without the user clicking on the notification.
Would anyone have instructions on how to redirect a with the app closed from the click on a specific notification?
PS: I know I need to send the data in the notification body, I just want to understand how to make the flutter capture the click with the app closed in version 9.0 or higher.
Upvotes: 1
Views: 2851
Reputation: 2110
Use FirebaseMessaging.instance.getInitialMessage()
method to get messages
If App is Closed/Killed
FirebaseMessaging.instance
.getInitialMessage()
.then((RemoteMessage message) {
print("FirebaseMessaging.getInitialMessage $message");
});
Use FirebaseMessaging.onMessageOpenedApp
method to get messages
If App is in background
FirebaseMessaging.onMessageOpenedApp
.listen((RemoteMessage message) {
print("FirebaseMessaging.onMessageOpenedApp $message");
});
Use FirebaseMessaging.onMessage
method will receive a message
If App is in foreground
FirebaseMessaging.onMessage
.listen((RemoteMessage message) {
print("FirebaseMessaging.onMessage $message");
});
Upvotes: 5