Although all the other functions work perfectly, I receive the message and display it in all conditions, in the background, foreground or initial message.
in the background:
Future<void> backgroundHandler(RemoteMessage message) async {
print(message.data.toString());
print(message.notification!.title);
}
FirebaseMessaging.onBackgroundMessage(backgroundHandler);
in the foreground:
FirebaseMessaging.onMessage.listen((message) {
if (message.notification != null) {
add(NotificationRecieved(message));
}
});
in the initialization of the application:
await FirebaseMessaging.instance.getInitialMessage().then((message) {
if (message != null) {
LocalNotificationService.display(message);
return message.data["route"];
}
});
but this doesn’t work: when I click on the notification
FirebaseMessaging.onMessageOpenedApp.listen(
(message) {
toast(message.data.toString());
var route = message.data["route"];
Navigator.pushNamed(context, route);
},
);
the function (onMessageOpenedApp.listen) itself isn’t being called.
4
Answers
Do Checking Like This:
Try this:
I hope it will be helpful.
First in method to show push notification add reader of a payload.
Then somewhere in the application for example in initState method try to call the payload.
Be aware that flutterLocalNotificationsPlugin is properly initialized in main().
onMessageOpenedApp this will work only once your app is in background state (not terminated)
For foreground state you can use local notification plugin like this
Pass payload data while showing notification with local notification plugin example below