#contents

*Google API Playground [#i5f7a130]

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

*Firebase V1 API [#gca2df37]

**Google公式ブログ [#wf4f6b06]

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

**レスポンスJSON 異常系 [#gd96d39d]

***GoogleAPIの認証がNG [#y644b10e]

アクセストークンの有効期間はリクエストしてから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"
	    }
	}

***送信したパラメータ名にエラーあり [#r4ac5ee3]

    {
        "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."
                        }
                    ]
                }
            ]
        }
    }




***送信したパラメータの中身にエラーあり [#za165a1e]

-tokenが存在しない場合もこのエラー。せめて送信対象デバイスがありませんとでも出てくれれば良いものが、単に引数が不正ですと言われ不親切でよくわからない

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


**送信API [#a14333cb]

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


***有効化 [#de50e16a]

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

***リクエストを投げてみる [#lcf24448]

 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 [#hc98968f]

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

***トークンの有効期限切れエラー [#q28fc2f8]

 {
    "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 [#g4caa748]

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


*Hosting [#l076f6cb]

 firebase init
 firebase deploy

*Analytics [#pb4ed0a3]

*Database [#r62f08fb]

*Storage [#o94c5a55]

*Crash Reporting [#a011b30e]


*Firebase Cloud Messaging [#t7ad7a90]

-宛先端末の登録トークンを各デバイスごとのSDKにて生成しておく。
-管理側は登録トークンを指定してメッセージ送信

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

**構成要素 [#i56e1469]

|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 [#w24b2887]

|Android|152バイト|

**Curlによる送信 [#xd051827]

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

-グループ送信

 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 [#occ2607a]

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

**payload [#sa1ee35c]

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



*GitHub [#cd3f8bda]

**ブラウザ [#s37754c1]

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

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