Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Troubleshoot Android GCM 401 error?

I'm getting many 401 Unauthorized errors when trying to connect to Google Cloud Message Server.

Our code was working for some time on our test server but on the production one it fails some times.

The API key was working ok, the push notifications arrive (eventually), but meanwhile I get a lot of 401 errors.

I've verified on the console that server IP address is on the white list:

When following Google Troublesooting guide, I get surprising results:

me@myserver:~$ :curl --header "Authorization: key=AIzaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxFFU" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send  -d "{\"registration_ids\":[\"ABC\"]}"
<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
me@myserver:~$ curl --header "Authorization: key=AIzaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxFFU" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send  -d "{\"registration_ids\":[\"ABC\"]}"
{"multicast_id":7560936933053964665,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
me@myserver:~$ curl --header "Authorization: key=AIzaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxFFU" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send  -d "{\"registration_ids\":[\"ABC\"]}"
<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>

Sometimes, the same curl command works and sometimes it doesn't without any change nor in the console nor in the command line... As sometimes it works, I asume the key it's ok (I've double checked the key and the allowed IPs several times).

I have checked all the stackoverflow questions and tested all the suggestions, but even removing all the IPs from the white list (to allow requests from any IP) the behavior is still the same, some times it works some times it doesn't

Some references I've checked:

  • GCM with PHP (Google Cloud Messaging)
  • GCM http 401 authorization error
  • Unreliable 401 errors with GCM services
  • GCM send message fails with 401 (Unauthorized)

Any advice on what can be going on would be very appreciated.

like image 667
pconcepcion Avatar asked Mar 06 '14 03:03

pconcepcion


People also ask

How do I enable GCM on Android?

Open your app's settings and select Intercom for Android. Then find the "Enable GCM" section.

What is GCM service on Android?

Google Cloud Messaging (GCM) is a service that allows you to send push notifications from your server to your users' Android devices, and also to receive messages from devices on the same connection.

Is GCM reliable?

We evaluate GCM in real world experiments, and at a reasonable scale involving thousands of real users. Our findings reveal that the GCM message delivery is unpredictable, namely having a reliable connection to Google's GCM servers on the client device does not guarantee a timely message arrival.

How does a GCM work?

GCM stands for Google Cloud Messaging. Every push notification receive on any Android device is sent by the GCM only. when sender sends an push notification then it goes to GCM. GCM receives that push and forward it to particular Android Device by its Unique device id.


1 Answers

After many checks and not finding a good result the only solution we could give was to create a new project (the 3rd one because we did it already before) with a new API key, and this 3rd time it works.

like image 175
pconcepcion Avatar answered Sep 22 '22 08:09

pconcepcion