Flutter : flutter_local_notifications onSelectNotification not working when app is terninated
2,174
Solution 1
var details = await NotificationService()
.flutterLocalNotificationsPlugin
.getNotificationAppLaunchDetails();
if (details.didNotificationLaunchApp) {
print(details.payload);
}
Use this code in the first page it can get notification tapped payload
Solution 2
The above solution is Good. But It is not enough for my application. My scenario is - after clicking on the notification it will redirect to a detail page. foreground & background is working fine but the problem was for the terminated application. So I did like -
Notification_plugin.dart
Future<void> showNotification(RemoteMessage message, [bool importance = true]) async {
dynamic notification = message.data;
final prefs = await SharedPreferences.getInstance();
// check message ID is valid or not
prefs.setBool('hasMsgId', message.messageId != null ? true : false);
await flutterLocalNotificationsPlugin.show(
message.hashCode,
notification['title'],
notification['body'],
_setPlatFormSpecificSettings(importance),
payload: notification['docId'],
);
}
ItemScreen.dart
@override
void initState() {
super.initState();
_runWhileAppIsTerminated();
}
void _runWhileAppIsTerminated() async {
var details = await notificationPlugin.flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
if (details.didNotificationLaunchApp) {
if (details.payload != null) {
final prefs = await SharedPreferences.getInstance();
if (prefs.getBool('hasMsgId')) {
prefs.remove('hasMsgId');
if (prefs.get('authId').toString() != null) {
Navigator.of(context).pushNamed(
ItemDetailsScreen.routeName,
arguments: details.payload,
);
} else {
Navigator.of(context).pushReplacementNamed(AuthScreen.routeName);
}
}
}
}
}
I hope it will be helpful for anyone. Thanks!!!
Author by
Muhammad
Updated on December 01, 2022Comments
-
Muhammad over 1 year
flutter_local_notifications is working fine when the app is in foreground or background
but when the app is terminated
onSelectNotification not working as expected , instead it's just open the application
static void initializeLocalNotification() async { FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); const AndroidInitializationSettings initializationSettingsAndroid = AndroidInitializationSettings('ic_launcher'); final IOSInitializationSettings initializationSettingsIOS = IOSInitializationSettings( requestSoundPermission: false, requestBadgePermission: false, requestAlertPermission: false, ); final InitializationSettings initializationSettings = InitializationSettings( android: initializationSettingsAndroid, iOS: initializationSettingsIOS, ); await flutterLocalNotificationsPlugin.initialize(initializationSettings, onSelectNotification: (String payload) async { SharedPreferences prefs = await SharedPreferences.getInstance(); var res = await prefs.setString(kNotificationPayLoad, payload); // navigatorKey.currentState // .push(MaterialPageRoute(builder: (_) => LoginScreen())); // navigatorKey.currentState.pop(); }); await flutterLocalNotificationsPlugin .resolvePlatformSpecificImplementation< IOSFlutterLocalNotificationsPlugin>() ?.requestPermissions( alert: true, badge: true, sound: true, ); }
-
Tushar Roy almost 3 yearsThis solution is Good. But it's not clearing the last payload & that's why if anyone launches this app after the termination you will see the last invoked payload.