我想通过手机向我的所有用户发送通知。我正在使用GCM和phonegap推送插件。我在数据库中保存了一个registrationId,然后我编写了这2个函数,并使用soap web服务工作。通知不发送。 这是我的代码。功能:发送推送通知给所有用户phonegap
function sendPush($registrationId, $title, $message) {
$registrationIds = array(
$registrationId
);
$msg = array(
'message' => $message,
'title' => $title,
'vibrate' => 1,
'sound' => 1
// you can also add images, additionalData
);
$fields = array(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array(
'Authorization: key='.
'AIzaSyD-sL8HiKyKRWdwxVMmTMYgkqOgVGOiNP8',
'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);
if ($result === false) die('Curl failed '.curl_error());
curl_close($ch);
return $result;
}
function push() {
$db = new PDO('mysql:host=localhost;dbname=testf', 'root', '');
$sql = "select * from client";
$texte = '<table>';
// $texte=array();
foreach($db - > query($sql) as $data) {
$texte = $texte.
'#'.$data["regId"];
}
$texte = $texte;
return $texte;
}
电话是:
$client = new nusoap_client('http://localhost/fou/server.php');
$title = $_POST['title'];
$message = $_POST['message'];
$result = $client->call('push');
//echo $result;
$x = explode("#", $result);
$nb = count($x);
for ($i = 0; $i < $v; $i++) {
$resp = $client->call('sendPush', array('registrationId' => $x[$i], 'titre' => $title, 'message' => $message));
print_r($resp);
}
你会得到一个响应JSON从谷歌在$结果? –
不,我没有得到$结果 – user1674906
的回应,你必须得到一些东西。我可以看到你没有打印或记录$ result.please的值打印/记录它并共享输出。 –