LoginSignup
1
0

More than 5 years have passed since last update.

【Clojure】FirebaseCloudMessagingでプッシュ通知を送る

Last updated at Posted at 2017-12-22

fcmでNotificationを送る時のメモ

送り方だけの説明です。その前の設定はこちら

要件

シンプルに、

  • 音鳴らしたい
  • バイブレーション鳴らしたい

データ構造

jsonで表すとこんな感じ

{
 "to": "ここにFCMトークンをいれる",
 "notification": {"body": "Hello from curl via FCM!", "sound": "default"},
 "priority": "high"
}

APIにする前にまずcommandlineでテスト

https://fcm.googleapis.com/fcm/send
にPOSTするだけ。


$ curl --header "Content-Type: application/json" \
--header "Authorization: key=あなたのサーバーキー" \
https://fcm.googleapis.com/fcm/send \
-d '{"notification": {"body": "Hello from curl via FCM!", "sound": "default"},
"priority": "high",
"to": "ここにFCMトークンをいれる"}'

serverKey は、

Firebaseコンソール -> Project Overview -> アプリの右上押して「設定」 -> 上部タブから「Cloudメッセージング」 -> 「サーバーキー」

に書いてあるトークンをコピペ。

ClojureでAPI作るならこんな感じで

(ns yourapp.notification
 [org.httpkit.client :as http]
 [clojure.data.json :as json])

(def RIZM-FCM-SERVER-KEY "あなたのサーバーキー") ;キー直書きは非推奨

(http/request
   {:url "https://fcm.googleapis.com/fcm/send"
    :method :post
    :headers {"Content-Type" "application/json"
              "Authorization" (str "key=" RIZM-FCM-SERVER-KEY)}
    :body (json/write-str {:to "送信先のtoken"
                           :priority "high"                           
                           :notification {:body "通知メッセージ" :sound "default"}})
    } (fn [{:keys [status headers body error]}]
        (when error (info "fcm error:" error))))

他使えるパラメータは下記「通知ペイロードのサポート」の内容を参考に。
https://firebase.google.com/docs/cloud-messaging/http-server-ref?hl=ja

1
0
1

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
1
0