[Flutter/dart] The method at notification tapping does not work when the app is terminated
Phenomenon
Create a notification using flutter_local_notification. The method when tapping the notification is also registered.
If the app is up (whether foreground or background) when the notification comes, it works as expected.
However, if the app is terminated when the notification arrives, tapping the notification will not result in the registered operation.
Cause and solution
The registered method will disappear when the application is terminated (reference).
Note: from version 4.0 of the plugin, calling initialize will not trigger the onSelectNotification callback when the application was started by tapping on a notification to trigger. Use the getNotificationAppLaunchDetails method that is available in the plugin if you need to handle a notification triggering the launch for an app e.g. change the home route of the app for deep-linking.
As a countermeasure, a method called getNotificationAppLaunchDetails () tells you whether the app was started by a notification tap, so if it starts from a notification tap, you can perform the desired action.
NotificationAppLaunchDetails _lanuchDeatil
=await flutterLocalNotificationsPlugin.launchedFromNotification();
if (_lanuchDeatil!=null){
if (_lanuchDeatil.didNotificationLaunchApp){
if (_lanuchDeatil.payload==null){
}
else {
・・・
}
}
}
}
If _lanuchDeatil.didNotificationLaunchApp is true, it is started by a notification tap, so perform what you want. At that time, _lanuchDeatil.payload contains the payload passed when creating the notification, so refer to it.
Recent Posts
See AllWhat want to do I want to create an input form using TextField. For example, if the input content is a monetary amount, I would like to...
What want to do There is a variable that remain unchanged once the initial value is determined. However, it cannot be determined yet when...
What want to do As the title suggests. Place two widgets in one line on the screen One in the center of the screen and the other on the...
Comments