我想使用3.3 cordova在Android中实现推送通知,我是新的。我尝试了几个教程但没有成功。 可以解释一个简单的方法来逐步实现它吗?
由于
答案 0 :(得分:2)
在您的cordova中添加此插件
cordova plugin add https://github.com/phonegap-build/PushPlugin.git
你可以使用php发送消息
在控制台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);
}
}
这个插件有很多例子。同样的https://github.com/phonegap-build/PushPlugin.git是文档