我正在使用新的谷歌云消息传递功能,它在客户端开发成功,可以接收推送通知,不会有任何脱落。但我在服务器上使用了一个旧的发送功能。现在我想使用PHP实现新的发送函数(XMPP)。
我也在这里注册了https://services.google.com/fb/forms/gcm/并从谷歌获得了回复邮件和密钥。
从那以后,我必须实现SmackCcsClientJava类和两个库。但我不知道如何将该文件托管到我的PHP服务器上。
经过一些研究,我得到了PHP的函数和PHP 的xmphp库
$conn=新的XMPPHP_XMPP($host,$port,$user,$password,$resource,$server、$printlog、$loglevel);
但不能取得成功,它说不能连接。
您可以使用这个类:
class gcm {
public static function send_notification($deviceid, $message) {
// include config
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => (is_array($deviceid) ? $deviceid : array($deviceid)),
'data' => array("message" =>$message),
);
$headers = array(
'Authorization: key=YOUR-AUTH-KEY',
'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);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
}
}