LoginSignup
12
7

More than 5 years have passed since last update.

FCM Topic Messaging の挙動

Posted at

概要

FCMではtopic宛にメッセージを送ることで、topicに登録した端末に一斉にpush通知を送ることができます。topicに端末を登録する際、端末のfcm registration token(以下TOKEN)を使用しますが、これはアプリを再インストールした場合等に更新されます。TOKENが更新された場合topic上でどう扱われるか気になったので、試してみました。

結論

TOKENが更新された場合、更新前に登録していたtopicからは通知が来なくなることがわかりました。ですが、topicい登録していた古いトークはどうなるんでしょうね?古いトークンを使って明示的にunsbscribeすることはできないようなのですが、更新されたら勝手にunsbscribeにしてくれているんですかね。。

実験

topicへ端末を登録

$ curl -XPOST --header "Authorization: key=${SERVER_KEY}" \
    --header "Content-type: application/json" \
    --header "Content-Length: 0" \
    "https://iid.googleapis.com/iid/v1/${TOKEN}/rel/topics/test"

登録状況の確認

$ curl --header "Authorization: key=${SERVER_KEY}" \
    "https://iid.googleapis.com/iid/info/${TOKEN}?details=true" | jq
{
  "applicationVersion": "1",
  "connectDate": "2018-07-12",
  "attestStatus": "NOT_ROOTED",
  "application": "com.example",
  "scope": "*",
  "authorizedEntity": "111111111111",
  "rel": {
    "topics": {
      "test": {
        "addDate": "2018-07-12"
      }
    }
  },
  "appSigner": "xxxxxxx",
  "platform": "ANDROID"
}

メッセージの送信

$ curl --header "Authorization: key=${SERVER_KEY}" \
    --header Content-Type:"application/json" \
    https://fcm.googleapis.com/fcm/send \
    -d "{
      \"to\" : \"/topics/test\", 
      \"data\" : {
        \"message\": \"hello\"
      }
    }"

受信できました。

アンインストール&再インストール&登録状況の確認

$ curl --header "Authorization: key=${SERVER_KEY}" \
    "https://iid.googleapis.com/iid/info/${TOKEN}?details=true" | jq
{
  "error": "No information found about this instance id."
}

$ curl --header "Authorization: key=${SERVER_KEY}" \
    "https://iid.googleapis.com/iid/info/${NEW_TOKEN}?details=true" | jq
{
  "applicationVersion": "1",
  "connectDate": "2018-07-12",
  "attestStatus": "NOT_ROOTED",
  "application": "harutacompany.sample.message",
  "scope": "*",
  "authorizedEntity": "366365399328",
  "appSigner": "c40ce9d10d3d1ac89519090ae031de3f1f50af42",
  "platform": "ANDROID"
}

メッセージの送信

$ curl --header "Authorization: key=${SERVER_KEY}" \
    --header Content-Type:"application/json" \
    https://fcm.googleapis.com/fcm/send \
    -d "{
      \"to\" : \"/topics/test\", 
      \"data\" : {
        \"message\": \"hello\"
      }
    }"

受信できませんでした。

12
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
7