FCM と Notification の併用をやめてバックグラウンド受信時にうれしがる

ややこしすぎな気がします。

この記事のタイトルも書いてて意味不明です。

以下のような、FCMにJSONをPOSTしてアプリで受ける場合の話です。


curl https://fcm.googleapis.com/fcm/send -X POST \
--header "Authorization: key=FirebaseConsoleのプロジェクトの設定、クラウドメッセージングのタブにあるサーバーキー" \
--Header "Content-Type: application/json" \
 -d '
 {
   "to":"/topics/all",
   "data":{
     "title":"New Notification!",
     "body":"Test"
   },
   "priority":"high"
 }'

このPOSTするJSONに含まれるデータ構造によって、アプリ側で受信する場合の処理の流れ変わって見通しが悪くなります。

きれいにまとめられています。

Firebase Notificationでアプリの状態による挙動の違いについて - Qiita

アプリがフォアグラウンドにある場合、このServiceのonMessageReceived()が呼び出されることになります。

アプリがバックグラウンドにある場合はこのonMessageReceived()は呼ばれることはなく、システム側で自動的にシステムトレイに通知を表示します。

"data"ブロックはConsoleからも含めることはできます(詳細オプションのカスタムデータで指定できる)。しかし、Consoleでは"notification"ブロックを省略することができません(メッセージ文を入力しないと送信できない)。

送信したメッセージに"data"ブロックがあっても、"notification"ブロックが存在する限り、アプリがバックグラウンドにある状態ではFirebaseMessagingServiceのonMessageReceived()は呼ばれません。

"data"ブロックの内容を受け取れないわけではありません。通知をクリックして起動したActivityのintentに含まれることになります。

"data"ブロックの内容の処理が、フォアグラウンド時に受け取ったときはFirebaseMessagingServiceで、バックグラウンドのときにはActivityでとなってしまって煩雑です。

実際に試してみると、これら書かれてることすごく良く分かります。

結局、この"notification"ブロックの特典としては、

アプリがバックグラウンド状態で受信した場合にのみ、アプリ側の実装なしで通知トレイに入る。

ということだけでしょうか、おおまかですが。

すべてのメッセージ受信時にonMessageReceived()が呼び出されれば、アプリがフォアグラウンドにあるかバックグラウンドにあるかを気にする必要がなくなります。

なるほど。

では、入れ子にしたらいいんじゃないかと。

例えば、


{
  // ...

  "notification": {
    "title" : "News!!",
    "body" : "New Product Release!!"
  },

  "data": {
    "user": "Yamada Taro",
    "age": 20
  },

  // ...
}

を以下のように入れ子にする。


{
  // ...

  "data": {
    "user": "Yamada Taro",
    "age": 20,

    "notification": {
      "title" : "News!!",
      "body" : "New Product Release!!"
    }

  },

  // ...
}

すべてサービスで受けることができます。


class  MyFirebaseMessagingService : FirebaseMessagingService() {

  override fun onMessageReceived(message: RemoteMessage) {

    // ...

    // 2層目以下はStringで送られてくるのでMap化
    message.data["notification"]?.let {
      val notification : Map<String, String> =
        Gson().fromJson(it, object : TypeToken<HashMap<String, String>>() {}.type)
      sendNotification(notification)
    }

  }

  // サンプルを参考に
  private fun sendNotification(notification: Map<String, String>) {
    val intent = Intent(this, MainActivity::class.java)
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

    val pendingIntent = PendingIntent.getActivity(this, 0, intent,
        PendingIntent.FLAG_ONE_SHOT)

    val channelId = getString(R.string.default_notification_channel_id)
    val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
    val notificationBuilder = NotificationCompat.Builder(this, channelId)
        .setSmallIcon(android.R.drawable.ic_notification_overlay)
        .setContentTitle(notification["title"])
        .setContentText(notification["body"])
        .setAutoCancel(true)
        .setSound(defaultSoundUri)
        .setContentIntent(pendingIntent)

    val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

    notificationManager.notify(0, notificationBuilder.build())
  }

quickstart-android/MyFirebaseMessagingService.java at master · firebase/quickstart-android

元々、Firebase てのは、敷居を下げてマーケティングなどに利用を広めようとしてるのかな。