1

Is there any reason why the GCM (Google Cloud Messaging) Service from Google suddenly returns a 401 for a request to send an Android push notification? The call looks like this

curl --header "Authorization: key=<my_android_key>" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send -d "{\"to\":\"/topics/my_topic\", \"data\":{\"category\":\"help\", \"title\":\"Test\", \"text\":\"Test\", \"param\":\"\"}}"

The result is

<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>

UPDATE:

Google names four possible causes for this error:

  • Authorization header missing or with invalid syntax in HTTP request.
  • Invalid project number sent as key
  • Key valid but with GCM service disabled.
  • Request originated from a server not whitelisted in the Server Key IPs.

I checked the validity of the API key in question here, and it seems to be invalid. The question is why? It used to work so far. Argh. :-/

0x4a6f4672
  • 27,297
  • 17
  • 103
  • 140

2 Answers2

2

Not sure why sending push notifications using the Android Key stopped working, since it used to work before. I managed to send a Push Notification to an Android app by using the Server key instead of the Android key like this:

curl --header "Authorization: key=<my_server_key>" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send -d "{\"to\":\"/topics/my_topic\", \"data\":{\"category\":\"help\", \"title\":\"Test\", \"text\":\"Test\", \"param\":\"\"}}"

Apparently this seems to be the correct method now for Android: Why Android key (api key) is invalid or that key is deprecated by GCM server?

I found a site which says that "Google made a change that stopped Android Keys from working for GCM/FCM for security reasons" https://groups.google.com/forum/#!msg/firebase-talk/4lCPr-UIuY0/ShWZk6z2AAAJ

So the answer seems to be to use the "Server Key" to send Android GCM messages (not the "Android Key").

Community
  • 1
  • 1
0x4a6f4672
  • 27,297
  • 17
  • 103
  • 140
1

It seems like you were not use server key from developer console for your application.

Oleg Skidan
  • 617
  • 6
  • 24