我在服务器端使用 PHP 编码: 参考:Android Hive 推送通知教程。
<?php
$apiKey = "AIza......"; // my API key
$registrationIDs = array("APA91b............" ); // my device reg id
// Message to be sent
$msg = "This is push notifications";
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => $msg )
);
$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;
if($result == 1){
print "\nSuccess";
} else {
print "\nError";
}
?>
执行上述脚本时的输出:
{"multicast_id":605543.......,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:137.... ..50534%978..ecd"}]} 错误
我在我的设备上收到“ null ”,而不是在服务器上设置消息...
请告知我是什么问题......