[英]How to create a push notification with cordova 3.3 step to step?
I want to implement push notifications in android using 3.3 cordova, I'm new at this. 我想使用3.3 cordova在Android中实现推送通知,这是我的新手。 I have tried several tutorials but without success.
我尝试了一些教程,但没有成功。 Could explain a simple way to implement it step by step?
能解释一步一步实现的简单方法吗?
Thanks 谢谢
Add this plugin in your cordova 将此插件添加到您的cordova中
cordova plugin add https://github.com/phonegap-build/PushPlugin.git
And you can use php to send the messages 而且您可以使用php发送消息
Replace api key generated by server key in console google cloud messaging. 替换由控制台Google云端消息中的服务器密钥生成的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);
}
}
There are alot of examples of this plugins. 此插件有很多示例。 Also in the same https://github.com/phonegap-build/PushPlugin.git is the documentation
同样在https://github.com/phonegap-build/PushPlugin.git中的是文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.