Google API Playground

https://developers.google.com/oauthplayground/

Firebase V1 API

Google公式ブログ

https://firebase.googleblog.com/2017/11/whats-new-with-fcm-customizing-messages.html

レスポンスJSON 異常系

GoogleAPIの認証がNG

アクセストークンの有効期間はリクエストしてから60分。

	{
	    "error": {
	        "code": 401,
	        "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
	        "status": "UNAUTHENTICATED"
	    }
	}

送信したパラメータ名にエラーあり

   {
       "error": {
           "code": 400,
           "message": "Invalid JSON payload received. Unknown name \"payloads\" at 'message.apns': Cannot find field.",
           "status": "INVALID_ARGUMENT",
           "details": [
               {
                   "@type": "type.googleapis.com/google.rpc.BadRequest",
                   "fieldViolations": [
                       {
                           "field": "message.apns",
                           "description": "Invalid JSON payload received. Unknown name \"payloads\" at 'message.apns': Cannot find field."
                       }
                   ]
               }
           ]
       }
   }

送信したパラメータの中身にエラーあり

	{
	    "error": {
	        "code": 400,
	        "message": "Request contains an invalid argument.",
	        "status": "INVALID_ARGUMENT"
	    }
	}

送信API

https://fcm.googleapis.com/v1/projects/プロジェクトID/messages:send

送信JSON

{

   "message": {
       "token": "トークン",
       "name": "message name",
       "notification": {
           "body": "Test message 201711281530",
           "title": "notification title"
       }
   },
   "validate_only": true

}

有効化

https://console.developers.google.com/apis/api/fcm.googleapis.com/overview?project=tf-quickstart

リクエストを投げてみる

curl -H "Content-type: application/json" -H "Authorization: Bearer トークン"  -X POST -d '{ "validate_only": false, "message" : { "token": "test"}}' https://fcm.googleapis.com/v1/projects/[project-id]/messages:send

認証はOAuth2.0

https://firebase.google.com/docs/cloud-messaging/auth-server?hl=ja

トークンの有効期限切れエラー

{
   "error": {
       "code": 401,
       "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
       "status": "UNAUTHENTICATED"
   }
}

Firebase CLI

npm install -g firebase-tools
firebase login とするとブラウザが立ち上がりログイン完了
新しくディレクトリを作り、その中でfirebase initすることで選択できる。。

Hosting

firebase init
firebase deploy

Analytics

Database

Storage

Crash Reporting

Firebase Cloud Messaging

http://qiita.com/flatfisher/items/31f5c9e21ae56cccff8d http://inside.pixiv.net/entry/2016/12/03/190000

構成要素

FCM クライアントAndroidやiOSやブラウザなどの通知を受け取るデバイス
サーバーキープロジェクトの設定→クラウドメッセージングで取得可能なAPIキー。間違っていると401である。
登録トークンアプリを初めて起動すると、クライアント アプリのインスタンスの登録トークンが FCM SDK によって生成されます。間違っているとInvalidRegistrationが戻る
collapsible message後書きされても問題ないメッセージ。スポーツアプリのスコア情報通知は最新のものだけあれば良い。ただしデバイス単位で4つまでしか使えないのでたくさんの種類の通知をしたい場合には向いてない
non-collapsible message後書きされないメッセージ。100を超えると全部消えるのでその場合はその発生をhandlerで受けて再同期を実装する
priorityメッセージの優先度。通知はデフォルトでHighであり、スリープジも受信できる状況
time_to_liveメッセージの生存期間。電話の着信通知などは短くして置かないと意味がない。
SENDER_ID複数の通知元からのメッセージをまとめたい場合に使う

送信予約完了してもあくまで送信予約がされたという状態。デバイスがオフラインモードだったりすると送信待ち状態となり、collapsibleの場合はこの時に上書きされる可能性がある。

デバイストークンID

Android152バイト

Curlによる送信

事前にサーバーキーが必要。

curl -H "Content-type: application/json" -H "Authorization:key=$KEY"  -X POST -d '{"to": "ユニークなID","data": {"hello": "This is a Firebase Cloud Messaging Device Group Message!"}}' https://fcm.googleapis.com/fcm/send
curl -H "Content-type: application/json" -H "Authorization:key=$KEY"  -X POST -d '{ "data": { "score": "5x1","time": "15:10"},"to" : "デバイストークンID"}' https://fcm.googleapis.com/fcm/send
curl -H "Content-type: application/json" -H "Authorization:key=$KEY"  -X POST -d '{ "to": "/topics/foo-bar","data": { "message": "This is a Firebase Cloud Messaging Topic Message!"}}' https://fcm.googleapis.com/fcm/send

notification

システム側が表出までハンドリングしてくれるもの。音やらIconやら設定可能

payload

データは任意のものが設定できる代わりに、受信時に自分で処理を記載する必要がある。

GitHub

ブラウザ

https://github.com/firebase/quickstart-js/tree/master/messaging


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS