简体   繁体   中英

Unable to receive messages sent by server in Android GCM

I have been trying to implement Android GCM service using http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/ as a reference. I am able to register the emulator at the database but any message sent from the server is not received at the emulator. Can someone tell me where I am going wrong?

Run following PHP file,

<?php
//request url
$url    = 'https://android.googleapis.com/gcm/send';

//your api key
$apiKey = 'YOUR API KEY';

//registration ids
$registrationIDs = array('First_Reg_Id','Second_Reg_Id','Third_Reg_Id');

//payload data
$data   = array('title' => 'hi',  'message' => 'Push notification');

$fields = array('registration_ids' => $registrationIDs,
                'data' => $data);

//http header
$headers = array('Authorization: key=' . $apiKey,
                 'Content-Type: application/json');

//curl connection
$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_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
 print_r($fields);
$result = curl_exec($ch);

curl_close($ch);

echo $result;
?>

Before that, Replace Your API key which is given by google. Replace First_Reg_Ids with your emulator's registration code. Remove others if not used.

You just need to run this PHP file and you will get message with title and message. Just parse it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM