Android 如何使用cordova 3.3逐步创建推送通知?
我想用3.3 cordova在android中实现推送通知,我是新手。我试过几次教程,但都没有成功。 你能解释一下一步一步实现它的简单方法吗Android 如何使用cordova 3.3逐步创建推送通知?,android,push-notification,google-cloud-messaging,cordova-3,Android,Push Notification,Google Cloud Messaging,Cordova 3,我想用3.3 cordova在android中实现推送通知,我是新手。我试过几次教程,但都没有成功。 你能解释一下一步一步实现它的简单方法吗 谢谢在您的cordova中添加此插件 cordova plugin add https://github.com/phonegap-build/PushPlugin.git 您可以使用php发送消息 在控制台google cloud messaging中替换由服务器密钥生成的api密钥 这个插件有很多例子。同样在同一个文档中 class Pusher
谢谢在您的cordova中添加此插件
cordova plugin add https://github.com/phonegap-build/PushPlugin.git
您可以使用php发送消息
在控制台google cloud messaging中替换由服务器密钥生成的api密钥
这个插件有很多例子。同样在同一个文档中
class Pusher
{
const GOOGLE_GCM_URL = 'https://android.googleapis.com/gcm/send';
private $apiKey;
private $proxy;
private $output;
public function __construct($apiKey, $proxy = null)
{
$this->apiKey = $apiKey;
$this->proxy = $proxy;
}
/**
* @param string|array $regIds
* @param string $data
* @throws \Exception
*/
public function notify($regIds, $data)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, self::GOOGLE_GCM_URL);
if (!is_null($this->proxy)) {
curl_setopt($ch, CURLOPT_PROXY, $this->proxy);
}
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHeaders());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->getPostFields($regIds, $data));
$result = curl_exec($ch);
if ($result === false) {
throw new \Exception(curl_error($ch));
}
curl_close($ch);
$this->output = $result;
}
/**
* @return array
*/
public function getOutputAsArray()
{
return json_decode($this->output, true);
}
/**
* @return object
*/
public function getOutputAsObject()
{
return json_decode($this->output);
}
private function getHeaders()
{
return [
'Authorization: key=' . $this->apiKey,
'Content-Type: application/json'
];
}
private function getPostFields($regIds, $data)
{
$fields = [
'registration_ids' => is_string($regIds) ? [$regIds] : $regIds,
'data' => is_string($data) ? ['message' => $data] : $data,
];
return json_encode($fields, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
}
}