[Flutter/dart] flutter_local_notificationの通知タップ時にアプリが終了しているとタップ時の動作が動かない
現象
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が入っているので、参照する。
最新記事
すべて表示現象 アプリ内にAdmobを追加して、アプリを起動すると、下記のエラーが発生 java.lang.RuntimeException: Unable to get provider com.google.android.gms.ads.MobileAdsInitProvider 原因 AndroidManifestの書き方が誤っていた。 <meta-data>はactivityと同じ階層にある必要が
概要 Uriを持っていて、Urlに変換したい場合の方法で少し手惑ったので共有します 方法 String url = uri.toString(); これだけです。 最後に ページを開くだけだとUriでもUrlでもいいんですが、WebViewはUrlを要求してくるんですよね。
問題 以前、日本語を含むURLを開くためには、エンコーディングしてやる必要がある、という記事を書きました。 しかし、すでにエンコーディングされているURLを再度エンコーディングしてしまうと、別のURLになってしまいます。 つまり、URLを開く処理の前に、 ・エンコーディングが必要なURLか ・すでにエンコーディングがされているか を判定しないといけないことになります。これは中々煩雑な処理です。 発