[Flutter/dart] flutter_local_notificationの通知タップ時にアプリが終了しているとタップ時の動作が動かない
- M.R

- 2022年4月4日
- 読了時間: 1分
現象
flutter_local_notificationを使って通知を作成。通知タップ時の動作も登録。
通知が来た時にアプリが立ち上がっている(foregroundでもbackgroundでも)と、期待通りの動作となる。
しかし、通知が来た時にアプリが終了していると、通知をタップしても登録した動作とならない。
原因と解決策
アプリが終了すると登録した動作は消えてしまうらしい(参考)。
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.
対策としては、getNotificationAppLaunchDetails()というメソッドにより、アプリが通知タップによって始まったか否かが分かるので、通知タップから始まった場合は所望の動作を実行すればよい。
NotificationAppLaunchDetails _lanuchDeatil
=await flutterLocalNotificationsPlugin.launchedFromNotification();
if (_lanuchDeatil!=null){
if (_lanuchDeatil.didNotificationLaunchApp){
if (_lanuchDeatil.payload==null){
}
else {
・・・
}
}
}
}_lanuchDeatil.didNotificationLaunchAppがtrueならば通知タップによって始まっているので、所望の動作を行う。その際、_lanuchDeatil.payloadに通知作成時に渡したpayloadが入っているので、参照する。






コメント