LoginSignup
3
3

More than 5 years have passed since last update.

新規でGCMのPush通知をやろうとしたらできなくなっていた顛末メモ

Posted at

2016年10月現在、GCM2系の実装に関して検証をしているときに陥ったことのメモです。

事象

GoogleAPIConsoleで新しくプロジェクトを作成し
Andoridアプリ側で gcm.register({プロジェクト番号}); でRegistrationIDを振り出した後
GCM経由でメッセージを送ろうとcurlをするも認証エラーになってしまう。

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の方法をチェックするが
それでも直らず :cry:

原因

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/sendhttps://fcm.googleapis.com/fcm/send
どちらのAPIでも送信することができます。
※おそらくGoogleのことなのでUIが変わる懸念があります、見当たらなければそれっぽいものを探して下さい:bow:

参考URL

3
3
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
3
3