This code will send a GCM message to multiple registration IDs via CURL. 

If you receive an "*Unavailable*" error code when you try to send a GCM:

*Generate a Browser API Key from the Google APIs Console*, and use it *instead 
of the server key* in the "Authorization" header. Once you do that, this 
error will go away.

This is caused by a serious mistake in the GCM Documentation that states 
you should use a Server Key in the Authorization header (as written 
here<http://developer.android.com/guide/google/gcm/gs.html>
)

// Replace with real server API key from Google APIs
$apiKey = "123456";

// Replace with real client registration IDs 
$registrationIDs = array( "123", "456" );

// Message to be sent
$message = "x";

// Set POST variables
$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'
);

// Open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
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_POSTFIELDS, json_encode( $fields ) );

// Execute post
$result = curl_exec($ch);

// Close connection
curl_close($ch);
 echo $result;

On Friday, June 29, 2012 5:22:02 AM UTC+3, RyanMC wrote:
>
> We are in the final stages of developing an app that had utilized C2DM for 
> push notification, and decided to swap over to the new GCM since we hadn't 
> released yet and it seemed like a good idea. 
>
> However, this is proving to be a giant headache. We have the JSON and 
> headers formatting correctly and we get back a valid response, but the 
> failed count is always 1 and the error message is UNAVAILABLE. The 
> documentation says to just try again, but this has proved pointless as it 
> always returns the same result. I have seen several posts on stackoverflow 
> and other sites that list a similar problem. Has anyone been able to get 
> the new GCM stuff working? We are using a php backend to handle the push 
> notifications. We are using the standard CURL library, and as far as I can 
> tell the JSON is valid, and works as we do get a result. Prior to swapping 
> out the old message attribute with the new data attribute it would fail to 
> return any result, so I am pretty confident we have it right. 
>
> Any thoughts would be appreciated. 
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to