【问题标题】:Send push notification to all user phonegap向所有用户 phonegap 发送推送通知
【发布时间】:2016-05-10 07:24:53
【问题描述】:

我正在尝试通过电话向我的所有用户发送通知。我正在使用 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);
}

【问题讨论】:

  • 您在 $result 中收到来自 google 的响应 JSON 吗?
  • 不,我没有收到 $result 的回复
  • 你一定得到了什么。我可以看到您没有打印或记录 $result 的值。请打印/记录它并共享输出。
  • 我打印但没有输出结果。 " $var= $client->call('sendPush',array('registrationId'=>$x[$i],'title'=>$title,'message'=>$message)); print_r($var );"

标签: php android cordova push-notification


【解决方案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);
?>

并从cordova 应用程序中调用php 文件,例如:

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

【讨论】:

  • 我有一个表格来写消息和标题,我编辑你的代码。我删除了 2 第一行。我用 pdo 工作,我写了 " $query = "SELECT regId FROM client"; $stmt = $db->prepare($sql); $stmt->execute(); // 设置数组 $registrationIds = array(); // 查看查询 while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { // 将返回的每一行添加到数组中 $registrationIds[] = $row; } " 我调用 " $var= $ client->call('sendPush',array('title'=>$title,'message'=>$message)) echo $var;"我有一个意外的“回声”错误
  • 打印函数结果时,函数调用出错。这是我的电话pastebin.com/hysvRssU
  • 但我不明白你为什么要使用 SOAP?您只需通过从表单获取数据将您的消息和标题直接传递给函数。
  • 我使用 SOAP 因为我的应用程序是使用 SOAP 和 php 开发的。你的意思是我没有将我的功能集成到我的 SOAP 服务器中
  • 是的。使用表单的序列化方法直接调用您的php 文件。因此,它会发布您的所有值,例如 msg 和 title。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多