30

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); }
curl_close($ch);
echo $result;

Please help me for this issue. Thanks in advance.

Sushil Kandola
  • 870
  • 2
  • 9
  • 22
  • refer this http://stackoverflow.com/a/21628030/1765530 – appukrb Feb 07 '14 at 12:41
  • can u make a small step list of google configuration? – valeriocomo Apr 15 '15 at 19:09
  • For others who are browsing just now, if you've suddenly started experiencing this error without making any specific changes (the Server Key) still the same, you probably need to import your project to Firebase. See my answer [here](http://stackoverflow.com/a/37801206/4625829). – AL. Oct 25 '16 at 08:21

13 Answers13

31

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

Jos
  • 1,015
  • 2
  • 13
  • 25
  • 3
    Thank you all guys. I was facing the problem due to browser key and API key. Now its working great with Server API key. – Sushil Kandola Jan 16 '14 at 11:34
  • Hello Jos. How to did I Whitelist the IP of my server. ? I get unauthorized 401. – Olkunmustafa Mar 17 '14 at 13:27
  • 2
    Google updated the console a bit, you will be redirected following the link in my answer. You can see the button "Edit allowed IPs" there. I have put in to "Any IP allowed", depending on the secrecy of my key for security. – Jos Mar 18 '14 at 09:16
19

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.

DanO
  • 10,230
  • 4
  • 41
  • 34
  • 5
    If you're using curl for debugging, and have whitelisted your IPv4 address but not your IPv6 address, you can pass `-4` to curl to have it connect using IPv4. – John Mellor Oct 28 '14 at 14:56
  • 1
    More specifically, add this curl option: `curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );` – zeusstl Feb 05 '15 at 07:30
  • The equivalent of 0.0.0.0/0 (all IPv4 addresses) is ::/0 for IPv6, FYI – four43 Mar 13 '15 at 15:58
14

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!

Dan Bartlett
  • 173
  • 1
  • 7
3

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:

curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
Thomas Decaux
  • 21,738
  • 2
  • 113
  • 124
2

use browser key using option create new browser key and use this key in your code. https://code.google.com/apis/console/?pli=1#project:42053154xxxx:access

mayur bhagat
  • 209
  • 3
  • 12
  • This is what my problem was, I was trying to use the server key instead of the browser key, I guess it makes more sense as Curl is more like a web browser rather than a direct socket connection. Can't believe how much time I loss, just trying everything except this /facepalm – CTS_AE Dec 19 '13 at 21:42
2

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

m1uan
  • 528
  • 6
  • 16
1

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. \

Nir
  • 24,619
  • 25
  • 81
  • 117
0

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

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

Case
  • 4,244
  • 5
  • 35
  • 53
0

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:

 @Override
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);
}
Lettings Mall
  • 300
  • 3
  • 10
0

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.

Qin Zhengquan
  • 467
  • 1
  • 8
  • 20
0

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

Pavan Pyati
  • 950
  • 2
  • 13
  • 18
0

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.

-1

Make sure you enabled your API Survery Key!

John Slegers
  • 45,213
  • 22
  • 199
  • 169
winston
  • 5
  • 4