2016年10月現在、GCM2系の実装に関して検証をしているときに陥ったことのメモです。
事象
GoogleAPIConsoleで新しくプロジェクトを作成し
Andoridアプリ側で gcm.register({プロジェクト番号});
でRegistrationIDを振り出した後
GCM経由でメッセージを送ろうとcurlをするも認証エラーになってしまう。
$ curl --header "Authorization: key={新しく作ったプロジェクトのAPIKey}" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send -d "{\"registration_ids\":[\"{振り出したRegistrationID}\"],\"data\":{\"id\":\"1\",\"message\":\"Test\"}}"
<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
従来401エラーはAPIKeyの誤りなので、コピーミスやcurlの方法をチェックするが
それでも直らず
原因
2016年の9月以降はFirebase経由で振り出したAPIKey以外は使えないようにしたみたいです。
Starting from Sept. 2016 new server key can only be created in the Firebase Console using the Cloud Messaging tab of the Settings panel. Existing projects that need to create a new server key can be imported in the Firebase console without affecting their existing configuration.
HTTP Connection Server Reference | Cloud Messaging | Google Developers
解決法
既存のプロジェクトをFirebaseのプロジェクト作成でインポートできるので
そちらでインポートした後
「プロジェクトの設定」→「クラウドメッセージング」→「サーバーキー」の文字列を
APIKeyとして指定し、Registration IDを送信先として指定すれば
https://android.googleapis.com/gcm/send
、 https://fcm.googleapis.com/fcm/send
どちらのAPIでも送信することができます。
※おそらくGoogleのことなのでUIが変わる懸念があります、見当たらなければそれっぽいものを探して下さい