diadia

興味があることをやってみる。自分のメモを残しておきます。

FirebaseCloudMessagingによるプッシュ通知を受信してもアプリを起動できない件

理由がわからない。 サンプルで作ったアプリはプッシュ通知をタップするとアプリが立ち上がったけど、今のアプリは立ち上がらない。

Firebase Notifications は、受信側アプリがフォアグラウンド状態であるかバックグラウンド状態であるかによって、動作が異なります。

フォアグラウンドって具体的にどんな状態を示すのか分かっていない。

フォアグラウンドとバックグラウンドで設定する内容が異なるようだ。
フォアグラウンドの場合には以下が必要。

<service
     android:name=".MyFirebaseMessagingService">
     <intent-filter>
         <action android:name="com.google.firebase.MESSAGING_EVENT"/>
     </intent-filter>
</service>

それでMyFirebaseMessagingServiceのサービスを作成すれば良い。

バックグラウンド用には、

<meta-data
    android:name="com.google.firebase.messaging.default_notification_channel_id"
    android:value="@string/default_notification_channel_id"/>
<!-- Set custom default icon. This is used when no icon is set for incoming notification messages.
     See README(https://goo.gl/l4GJaQ) for more. -->
<meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@drawable/ic_launcher_foreground" />
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
     notification message. See README(https://goo.gl/6BKBk7) for more. -->
<meta-data
    android:name="com.google.firebase.messaging.default_notification_color"
    android:resource="@color/colorAccent" />

<string name="default_notification_channel_id" translatable="false">my_app_fcm_default_channel</string>

これらが必要になる。