2016-05-10 59 views
-1

我想通過手機向我的所有用戶發送通知。我正在使用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); 
} 
+0

你會得到一個響應JSON從谷歌在$結果? –

+0

不,我沒有得到$結果 – user1674906

+0

的迴應,你必須得到一些東西。我可以看到你沒有打印或記錄$ result.please的值打印/記錄它並共享輸出。 –

回答

1

嘗試這樣的:

PushNotification.php

<?php 

    $message = $_REQUEST['msg']; 
    $title = $_REQUEST['title']; 

    $user = "root"; 
    $pass = ""; 

    // Connect 
    $db = new PDO("mysql:host=localhost;dbname=pdo-me", $user, $pass); 
    define('API_ACCESS_KEY', 'AIzaSyD-sL8HiKyKRWdwxVMmTMYgkqOgVGOiNP8'); 

    $msg = array(
     'message' => $message, 
     'title' => $title, 
     'vibrate' => 1, 
     'sound' => 1 
    ); 

    $query = "SELECT regId FROM client"; 
    $stmt = $db->prepare($query); 
    $stmt->execute(); 
    // set array 
    $registrationIds = array(); 


    // look through query 
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
     // add each row returned into an array 
     $registrationIds[] = $row; 
    } 

    $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); 
    print_r($result); 
?> 

並調用php文件從cordova應用,如:

http://yoururl.com/PushNotification.php?msg=test&title=LoremIpsum 

因此,在這種情況下,它將從您的表中檢索所有registrationId,並且它將向所有設備發送通知。

編輯1:

使用此代碼在您的應用程序。

//Getting value of title textbox 
var title = document.getElementById('title').value; 
//Getting value of message textbox 
var message = document.getElementById('message').value; 
$.ajax({ 
    type:'POST', 
    url: 'http://yoururl.com/PushNotification.php?title='+title+'&msg='+message, 
    success: function(data){ 
     alert("Success: " + data); 
    }, 
    error:function(jqXHR, textStatus, errorThrown){ 
     alert("Error"); 
    } 
}); 
+0

我有一個表單來編寫消息和標題,我編輯你的代碼。我刪除了第一行。我寫了「$ query =」SELECT regId FROM client「; $ stmt = $ db-> prepare($ sql); $ stmt-> execute(); //設置數組 $ registrationIds =陣列(); //期待通過查詢 而($行= $ stmt->取(PDO :: FETCH_ASSOC)){// 添加每一行返回到一個數組 $ registrationIds [] = $行; ('title'=> $ title,'message'=> $ message))「 \t echo $ var;」我有一個意想不到的錯誤'echo' – user1674906

+0

你可以在http://pastebin.com/上發佈你的完整代碼嗎? – Dhruv

+0

這是我的代碼:http://pastebin.com/fsmNV598 – user1674906