向所有用户phonegap发送推送通知

时间:2016-05-10 07:24:53

标签: php android cordova push-notification

我试图通过电话向所有用户发送通知。我正在使用GCM和phonegap推送插件。我在一个数据库中保存了一个registrationId然后我写了这个2函数,我使用soap web服务。通知不会发送。 这是我的代码。功能:

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

1 个答案:

答案 0 :(得分: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应用程序调用{​​{1}}文件,如:

cordova

因此,在这种情况下,它将从您的表中检索所有registrationId,它将向所有设备发送通知。

修改1:

在您的应用程序中使用此代码。

http://yoururl.com/PushNotification.php?msg=test&title=LoremIpsum
相关问题