[英]Android phonegap send push notification using Google Cloud Messaging using php
I have developed the android application in cordova 3.4 now I want to get the automatic notification to their mobile. 我已经在cordova 3.4中开发了android应用程序,现在我想将自动通知发送到他们的手机。 For this I refer websites as below
为此,我参考以下网站
I read all above links then did all stuff they told like create project on google cloud messaging , get the project id , create server key (In textbox I given my private server IP) then API Key, I also have registration id device and I wrote code in php for sending push notification to my device but it is giving me " unauthorised Error 404 " 我阅读了所有上述链接,然后做了所有他们告诉的事情,例如在google cloud消息上创建项目,获取项目ID,创建服务器密钥(在文本框中,我给了我的私有服务器IP),然后是API密钥,我也有注册ID设备,我写道php中用于将推送通知发送到我的设备的代码,但它给了我“ 未经授权的错误404 ”
my code is as below 我的代码如下
define( 'API_ACCESS_KEY', 'my api key' );
//$registrationIds = array( $_GET['id'] );
$registrationIds = array($id);
//var_dump($registrationIds);die;
//prep the bundle
$msg = array
(
'message' => 'New Jobs is updated',
'title' => 'get the job in cad',
'subtitle' => 'get the job in cad',
'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
'vibrate' => 1,
'sound' => 1
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
I just hit url and send the registration id of device to the my php page but this page give me the error unauthorised Error 401 . 我只是点击url并将设备的注册ID发送到我的php页面,但是此页面给了我错误未经授权的错误401 。
Que.1. 队列1。 Is this possible to send the push notification from php which is hosted on private server?
是否可以从托管在专用服务器上的php发送推送通知?
Que.2 Is device registration id compulsory for sending push notification to those user they installed the apps? Que.2是否必须向要安装应用程序的用户发送推送通知的设备注册ID?
Please anybody help me how to solve this above error and if anybody have another solution then tell me. 请有人帮助我如何解决上述错误,如果有人有其他解决方案,请告诉我。 I tried from yesterday but not achieve my goal so please help me.
我从昨天开始尝试,但是没有实现我的目标,所以请帮助我。
Here is the GCM code and its working properly with my device though its in CI framework but hope you can use it. 这是GCM代码,尽管它在CI框架中,但仍可与我的设备正常工作,但希望您可以使用它。 It is compulsory to add device_id for the push and also the passphrase you'll get for the site.
必须为该推送添加device_id,还必须为该站点添加密码。 just try it.
去尝试一下。
function sendNotification($device_id,$message) {
/*echo $device_id;
echo "<br>";
echo $message;*/
// note: you have to specify API key in config before
$this->load->library('gcm');
// simple adding message. You can also add message in the data,
// but if you specified it with setMesage() already
// then setMessage's messages will have bigger priority
$msg = $this->gcm->setMessage($message);
//var_dump($message);
// add recepient or few
$this->gcm->addRecepient($device_id);
//var_dump($device_id);
// set additional data
//$this->gcm->setData($params);
// also you can add time to live
$this->gcm->setTtl(500);
// and unset in further
//$this->gcm->setTtl(false);
// set group for messages if needed
//$this->gcm->setGroup('Test');
// or set to default
//$this->gcm->setGroup(false);
// send
return $this->gcm->send();
}
Hope it'll solve your problem. 希望它能解决您的问题。
<?php
$register_keys = array("YOUR_REGISTRY_KEYS")
$google_api_key = "YOUR_API_KEY";
$registatoin_ids = $register_keys;
$message = array("price" => $message);
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
$headers = array(
'Authorization: key=' .$google_api_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));
}
//print_r($result);
// Close connection
curl_close($ch);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.