Below is the exception I am getting even though my API key for server and browser application are valid. I checked it using curl. I send GCM request in both format UTF-8 and
In my case the problem was that I forgot to redeploy after setting correct API key in the datastore.
Please follow as par below instruction it's may help you.
Update: It would also seem that Migrating from GCM to FCM fixes the issue for 401 Unauthorized Error.
If you are just starting to use GCM, instead of creating a project in the Google Developers Console, do it in the Firebase Console. After creating the project, simply use the auto-generated Server Key. Here are the steps where to find the Server Key:
For old GCM projects, you can simply Import the project to the Firebase Console:
Here are the possible causes for 401 error when using GCM:
The sender account that you're trying to use to send a message couldn't be authenticated. Possible causes are:
Check that the token you're sending inside the Authorization header is the correct API key associated with your project.
Source: https://developers.google.com/cloud-messaging/http-server-ref#error-codes