GCM Phonegap PHP推送通知Android


GCM Phonegap PHP Push Notification Android

我正在开发我的个人应用程序使用Cordova (Phonegap),谷歌云消息和使用PHP作为我的服务器。我希望运行我的通知到许多设备,但我的问题是,我无法从我的PHP脚本发送通知到其他设备。如果我在自己的设备上使用自己的注册id,并在终端上运行PHP推送通知,那么只有我的设备会提示该通知。我的问题是,如果我想看到$gcm_header的所有注册id,并在$registrationIds列出,我需要注册到另一个设备吗?因为此时,我只在自己的设备上运行应用程序。下面是我的代码:

<?php
$gcm_header = 'GOOGLE-API';
$registrationIds = array($gcm_header);
//$registrationIds = array('my-regid-from-my-actual-device');
$msg = array('message' => 'Test message',
    'title' => 'Test title',
    'subtitle' => 'test subtitle',
    'tickleText' => 'test tickleText',
    'vibrate' => 1,
    'sound' => 1);
$fields = array('registration_ids' => $registrationIds, 'data' => $msg);
$headers = array('Authorization: key='. $gcm_header, '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);
echo $result;?>

需要一个数据库表来存储所有注册用户的注册id。一旦完成,你的服务器将能够发送推送通知到所有的设备,这些设备都是由表中唯一的注册id注册的。您可以编写一个register.php文件,该文件将包含用于注册用户并将其存储到数据库中的代码。

register.php

<?php
// response json
$json = array();
/**
 * Registering a user device
 * Store reg id in users table
 */
if (isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["regId"])) {
    $name = $_POST["name"];
    $email = $_POST["email"];
    $gcm_regid = $_POST["regId"]; // GCM Registration ID
    // Store user details in db
    include_once './db_functions.php';
    include_once './GCM.php';
    $db = new DB_Functions();
    $gcm = new GCM();
    $res = $db->storeUser($name, $email, $gcm_regid);
    $registatoin_ids = array($gcm_regid);
    $message = array("product" => "shirt");
    $result = $gcm->send_notification($registatoin_ids, $message);
    echo $result;
} else {
    // user details missing
}
?>