2016-05-10 11 views
-1

Ich versuche, eine Benachrichtigung an alle meine Benutzer mit Telefon zu senden. Ich verwende ein GCM- und Phonegap-Push-Plugin. Ich speichere eine Registrierungs-ID in der Datenbank und dann schreibe ich diese 2 Funktion und ich arbeite mit Soap-Web-Service. Die Benachrichtigung wird nicht gesendet. Das ist mein Code. Die Funktion:Senden Push-Benachrichtigung an alle Benutzer 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; 
} 

Der Aufruf ist:

$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

erhalten Sie eine Antwort JSON von Google in $ Ergebnis? –

+0

Nein, ich bekomme keine Antwort von $ result – user1674906

+0

Sie müssen etwas bekommen. Ich kann sehen, dass Sie den Wert von $ result.please nicht ausdrucken oder protokollieren, drucken/protokollieren und die Ausgabe freigeben. –

Antwort

1

wie Try this:

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); 
?> 

Und rufen, dass php Datei aus cordova Anwendung wie:

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

Also in diesem Fall wird es alle Registrierungs-ID aus Ihrer Tabelle abrufen und es wird Benachrichtigung an alle diese Geräte senden.

Edit 1:

Verwenden Sie diesen Code in Ihrer Anwendung.

//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

Ich habe ein Formular, um eine Nachricht und Titel zu schreiben, ich bearbeite Ihren Code. Ich lösche die 2 erste Zeile. Ich arbeite mit pdo, schreibe "$ query =" SELECT regID FROM client "; $ stmt = $ db-> vorbereiten ($ sql); $ stmt-> execute(); // set array $ registrationIds = array(); // Blick durch Abfrage while ($ row = $ stmt-> holen (PDO :: FETCH_ASSOC)) {// hinzufügen jede Zeile in eine Array zurückgegeben $ registrationIds [] = $ row; } "und ich rufe" $ var = $ client-> call ('sendPush', array ('title' => $ title, 'message' => $ message)) \t echo $ var; " Ich habe einen Fehler unerwartet 'Echo' – user1674906

+0

Können Sie bitte Ihren vollständigen Code auf http://pastebin.com/ veröffentlichen? – Dhruv

+0

Das ist mein Code: http://pastebin.com/fsmNV598 – user1674906

Verwandte Themen