首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GCM Phonegap PHP推送通知Android

GCM Phonegap PHP推送通知Android
EN

Stack Overflow用户
提问于 2015-05-16 00:36:34
回答 1查看 1.5K关注 0票数 0

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

代码语言:javascript
复制
<?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;?>
EN

回答 1

Stack Overflow用户

发布于 2015-05-19 08:35:03

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

register.php

代码语言:javascript
复制
<?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
}
?>

有关更详细的描述,您可以查看此tutorial

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30264576

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档