Home » Php » php – Google GCM server returns Unauthorized Error 401

php – Google GCM server returns Unauthorized Error 401

Posted by: admin April 23, 2020 Leave a comment


I am using GCM services to push information from server. If I use browser key it shows the sucess mesaage as : {“multicast_id”:4849013215736515938,”success”:1,”failure”:0,”canonical_ids”:0,”results”:[{“message_id”:”0:1348742583011905%2adac3a0f9fd7ecd”}]}
but i did not get any notifications on device.
And if I use server key it shows Unauthorized Error 401. My code is shown below:

$apiKey = "xxxxx";
$registrationIDs = array("xxxxxxxx");
$message = "testing Process";
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
        'registration_ids'  => $registrationIDs,
        'data'              => array("message"=>$message),
$headers = array( 
        'Authorization: key=' . $apiKey,
        'Content-Type: application/json'
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($fields) );

$result = curl_exec($ch);
if(curl_errno($ch)){ echo 'Curl error: ' . curl_error($ch); }
echo $result;

Please help me for this issue. Thanks in advance.

How to&Answers:

Did you whitelist the IP of your server? This is not necessary by default for the browser key, but it is for the server key.

You can check it here:

https://code.google.com/apis/console/#project:[YOUR PROJECT NUMBER]:access


Another answer already suggested whitelisting the IP address, which seems kind of obvious. What fixed it for me was whitelisting my IPv6 address. That was the key! I hope this helps someone else.


I tried everything in this thread, and still no luck.

So I checked enabled APIs (APIs and Auth -> APIs, Enabled APIs), and realised that I had enabled Google Cloud Messaging for Chrome not Google Cloud Messaging for Android. As soon as I enabled the latter, it worked immediately.

Double check you have the right API enabled!


As many people wrote, you have to whitelist your server IPV4 and IPV6. If you want only IPV4, add this to your curl php init:



use browser key using option create new browser key and use this key in your code.


I used to put to the

$apiKey = "xxxxx"

like project number (project id) which i put to Android Client app, but i have been wrong in server have to be appkey from google cloud console, (where you activate Google Cloud Messaging for Android)

    ProjectXXX -> APIs & auth -> Registered Apps -> Web App -> Server Key
 -> Api key

in my case i had there default project ‘Service Account-project’ on platform: ‘Web’ but there was just Hosted Application section and no evidence about any api key.
but when i create new Web Apps called ‘My app’ which contained 4 sections OAuth 2.0 Client ID, Certificate, Server Key, Browser Key i finaly find the api key;-)

may just me who don’t know it but i believe this answer could also helps to some one


for me the problem was that you have to enable the api. Having an API set up is not enough. Check that Google Cloud Messaging for Android appears on the enabled APIs in APIs-> enabled APIs.

If its not click the API library tab in APIS and enable it.


If I am not mistake your APIKEY needs to be base64 encoded.

Also try var_dump($results) to see if you get information then.


Looking at the code, I recognise this is from a php-gcm sample posted somewhere online. Its quite nice and I can assure you that both browser keys and whitelisted ip’s (not IPV6 or bas64 apikey) work.

The reason why no message is shown is because the send notification function you’re using 'data' => array("message"=>$message)
targets a key of “message”, this is what you must pass to your pending intent i.e

notification.setLatestEventInfo(context, title, message, intent);

This will enable the intent to read the message contained under this key..the message itself is obtained from the GCMIntentService method pasted below:

protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "Received message");
    String message = intent.getExtras().getString("message");
    Log.d(TAG, "The intent contains: " + intent.getExtras());
    displayMessage(context, message);
    // notifies user
    generateNotification(context, message);


Here is the problem: I was using 2 servers, 1 staging and the other the production server. For my staging server, which was hosted using digital ocean, I entered the inet address in the Key for server applications allowed IP addresses, and it worked just fine.

However, it failed when I changed to my inet address from my production server. Instead, I had to use
inet6 addr: /64 Scope:Global for this to work. To get the value if you encounter the same issue, just enter ifconfig, and find the above entry. Enter that value in the allowed IP addresses and it will work fine.


You must have different project than the default one. Create a project & then create a key. Don’t use default project.


Had this trouble, I was using GCM (Google cloud messaging). But after Sep 2016, the server key on the GCM will not work, you have to use FCM (firebase cloud messaging). Create new server keys only in the Firebase Console using the Cloud Messaging tab of the Settings panel. I went over to the firebase console console.firebase.google.com (I had not used it yet) and it asked me to import my project. Suddenly there was this new server key back on the GCM console. If using GCM, use the “legacy key” you see listed there.


Make sure you enabled your API Survery Key!


We are looking for solutions in days. More specifically, add this curl option: curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );