我正在使用新的 Google Cloud 消息传递功能,它已在客户端成功开发并接收推送通知而不会掉线。但我在服务器上使用旧的发送功能。现在我想使用 PHP 实现新的发送功能 (XMPP)。
我也在这里注册了https://services.google.com/fb/forms/gcm/并从谷歌获得了回复邮件和 key 。
由此我了解到我必须实现 SmackCcsClient Java 类和两个库。但我不知道如何将该文件托管到我的 PHP 服务器。
经过一些研究,我得到了 PHP 的函数和 PHP 的 xmphp 库
$conn = new 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);
}
}
关于php - 如何使用 PHP 设置新的 Google Cloud Messaging API 服务器实现和托管?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20650326/