GCM with PHP (Google Cloud Messaging)云推送消息功能的例子

发布时间:2020-07-14编辑:脚本学堂
使用Google Cloud Messaging可以很方便的发送消息到手机客户端。注意:客户端连接Google服务器需要注册ID。

使用Google Cloud Messaging可以很方便的发送消息到手机客户端。
注意:客户端连接Google服务器需要注册ID。
相关代码:
 

复制代码 代码如下:

<?
$url = 'https://android.googleapis.com/gcm/send';
$serverApiKey = "YOUR API KEY AS GENERATED IN API CONSOLE";
$reg = "DEVICE REGISTRATION ID";

$data = array(
      'registration_ids' => array($reg),
      'data' => array('yourname' => 'Joe Bloggs')
);

print(json_encode($data));

$client = new Zend_Http_Client($url);
$client->setMethod('POST');
$client->setHeaders(array("Content-Type" => "application/json", "Authorization" => "key=" . $serverApiKey));
$client->setRawData(json_encode($data));
$request = $client->request('POST');
$body = $request->getBody();
$headers = $request->getHeaders();
print("<xmp>");
var_dump($body);
var_dump($headers);
?>