Help us understand the problem. What is going on with this article?

Firebase consoleで登録したFCM(プッシュ通知)で受け取れるデータメモ

FCM(Firebase Cloud Messaging)は、アプリがForegroundの場合と、Backgroundの場合でデータの受け取り方と形式が異なります。
その辺りを簡潔にまとめました。

公式リファレンスは「こちら」

アプリがForegroundの場合

FirebaseMessagingService#onMessageReceived で以下のデータを受信できます。
ちなみに、画面上に通知は表示されません。

============================
Class       : RemoteMessage
From        : xxxxxxxxxxxx
To          : null
MessageType : null
MessageId   : xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
SentTime    : 1575594729695
Priority    : 1
OriginalPriority : 1
Ttl         : xxxxx
CollapseKey : (アプリのパッケージ名)
============================
Class       : Notification ※RemoteMessage.getNotification()
ChannelId   : null
Title       : (設定したタイトル)
Body        : (設定したメッセージ)
ClickAction : null
Tag         : xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
============================
Class       : Map<String, String> ※RemoteMessage.getData()
Data :
{
    custom_key1=custom_value1,
    custom_key2=custom_value2,
    ...
}

"Data"は、Firebase consoleで入力した"Custom data"の Key / Value Map です。

アプリがBackgroundの場合

上記onMessageReceivedは呼ばれません。
代わりに、プッシュ通知をタップしてアプリを起動した場合のみ、情報を取得できます。

情報は android.intent.action.MAIN が指定された Activity で取得できます。
onCreate or onNewIntent で Intent.getExtras に Bundle(Key / Value)の形式で入っています。

{
    // RemoteMessage
    from=xxxxxxxxxxxx,
    google.message_id=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    google.sent_time=,
    google.delivered_priority=high,
    google.original_priority=high,
    google.ttl=,
    collapse_key=(アプリのパッケージ名),

    // Custom data
    custom_key1=custom_value1,
    custom_key2=custom_value2,
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away