我一直在尝试使用http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/作为参考来实施Android GCM服务。我能够在数据库中注册模拟器,但是在模拟器上没有收到从服务器发送的任何消息。有人能告诉我哪里出错了吗?
答案 0 :(得分:2)
运行以下PHP文件,
<?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;
?>
在此之前,请更换由谷歌提供的您的API密钥。将 First_Reg_Ids 替换为模拟器的注册码。如果不使用,请删除其他人
您只需要运行此PHP文件,您将收到带有标题和消息的消息。只需解析它。