LoginSignup
2
2

More than 3 years have passed since last update.

コマンドラインでFCMプッシュ通知を送る

Last updated at Posted at 2020-05-01

参考リンク

.env 設定ファイル


# Firebase Console > プロジェクトを設定 > Cloud Messaging > サーバーキー
FIREBASE_SERVER_KEY=''

# プッシュ通知を受け取るChromeのデバイストークン
IID_TOKEN=''

# トピック名
TOPIC_NAME=''

# 通知をクリックしたときの遷移先
CLICK_ACTION=https://xxxx.xxxx

トピックを購読する

source .env

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

./get.sh

トピックを購読解除する

source .env

curl -s -XDELETE --header "Authorization: key=${FIREBASE_SERVER_KEY}" \
  --header "Content-type: application/json" \
  --header "Content-Length: 0" \
  "https://iid.googleapis.com/iid/v1/${IID_TOKEN}/rel/topics/${TOPIC_NAME}"

./get.sh

ステータスを表示する

  • 結果がjsonで返ってくるので jq で見ると良いです
source .env

curl -s --header "Authorization: key=${FIREBASE_SERVER_KEY}" \
    "https://iid.googleapis.com/iid/info/${IID_TOKEN}?details=true" | jq

トピックに送る

source .env

curl \
        -s \
        -d "{ \
        \"to\": \"/topics/${TOPIC_NAME}\", \
        \"priority\": \"high\", \
        \"notification\": { \
            \"title\": \"テストTitle\", \
            \"body\": \"テストBody\", \
            \"icon\": \"https://via.placeholder.com/60x60.png?text=TEST\", \
            \"image\": \"https://via.placeholder.com/480x360.png?text=TEST\", \
            \"click_action\": \"${CLICK_ACTION}\" \
        } \
        }" \
        -H "Authorization: key=${FIREBASE_SERVER_KEY}" \
        -H "Content-type: application/json" \
        https://fcm.googleapis.com/fcm/send

ほか

  • 反応があればもうちょっと書くかもしれない
2
2
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
2
2