ヨシ!

今日もご安全に!

FlutterFire Cloud MessagingのonLaunch,onResumeの注意点

tl;dr

dataにkey:click_actionvalue:FLUTTER_NOTIFICATION_CLICKを入れろ

詳細

Flutter でFirebase Cloud Messaging(FCM)をの初期設定をしました.

github.com

そしてプッシュ通知を受信する際は以下のコードになります.

    _firebaseMessaging.configure(
        onMessage: (Map<String, dynamic> message) async {
          print("firebase onMessage Content = $message");
        }, onLaunch: (Map <String ,dynamic>message) async {
      print("firebase onLaunch Content = $message");
    }, onResume: (Map<String,dynamic> message) async {
      print("firebase onResume Content =$message");
    });

バイストークンを取得してFirebase Consoleから通知を送ります.

f:id:OhkuboSGMS:20200304174721p:plain

アプリがバックグランド時に通知を受け取ることができます.

f:id:OhkuboSGMS:20200304174942p:plain

AndroidManifest.xmlに適切に設定して,通知をタップするとアプリを開くことができます.

アプリがフォアグラウンド時,onMessageで通知の情報を受け取れましたが バックグラウンド時にシステム通知トレイからタップによるアプリ起動でonLaunch,onResumeが起動しない問題が発生.

解決

公式に書いてました.

Refer to the Firebase documentation about FCM for all the details about sending messages to your app. When sending a notification message to an Android device, you need to make sure to set the click_action property of the message to FLUTTER_NOTIFICATION_CLICK. Otherwise the plugin will be unable to deliver the notification to your app when the users clicks on it in the system tray.

github.com

dataにkey:click_actionvalue:FLUTTER_NOTIFICATION_CLICKを設定する必要があります.

Firebase Consoleだとこう

f:id:OhkuboSGMS:20200304180235p:plain

※typeは関係ないです.