'How to handle click on push-notification when application is in foreground?
I use the Firebase Cloud Messaging (FCM) in my Flutter project. The following packages:
firebase_messaging: ^8.0.0-dev.15
flutter_local_notifications: ^4.0.1+2
I know that I can handle click on push-notification when app is in background in the following way:
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
//TODO
});
and it works fine, but how to handle click on push-notification when application is in foreground? Can't find in the documentation as well. Could you please help me. Thanks in advance.
Solution 1:[1]
Use this dependency when the app is in foreground- https://pub.dev/packages/flutter_local_notifications
Edit
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
//TODO
});
You are already using this code for when the app is in background. See that you are using onMessageOpenedApp
. For firing the message when in foreground you can use onMessage
. Example-
FirebaseMessaging.onMessage.listen((message) {
LocalNotificationService.display(message);
});
Solution 2:[2]
you can set the callback function on initializing
await flutterLocalNotificationsPlugin.initialize(
const InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsIOS,
),
onSelectNotification: handleClickNotification,
);
handleClickNotification(String? payload) {
//your code
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | hyobbb |