本文介绍了发送推送通知使用GCM多台Android设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/?通过GCM发送推送通知。一切工作正常,但是,我能够发送推送通知,只是一个设备。注册其他设备替换previous设备的注册ID。我试着在http://javapapers.com/android/android-multicast-notification-using-google-cloud-messaging-gcm/?但它不工作。任何的建议是有很大的帮助。

I am following http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/? to send push notifications via GCM. Everything works fine but, i am able to send push notification to just one device. Registering another device replaces registration id of previous device. I tried solution provided by Shardool in http://javapapers.com/android/android-multicast-notification-using-google-cloud-messaging-gcm/? but its not working. Any suggestion would be of great help.

下面是我的gcm.php codeS的注册设备并发送推送通知,但只注册过一个单一的设备

Here are my gcm.php codes that registers device and sends push notifications but, only to a single device registered recently

gcm.php

<?php
//generic php function to send GCM push notification
function sendPushNotificationToGCM($registatoin_ids, $message) {
    //Google cloud messaging GCM-API url
    $url = 'https://android.googleapis.com/gcm/send';
    $fields = array(
        'registration_ids' => $registatoin_ids,
        'data' => $message,
    );
    // Google Cloud Messaging GCM API Key
    define("GOOGLE_API_KEY", "MY_KEY");
    $headers = array(
        'Authorization: key=' . GOOGLE_API_KEY,
        'Content-Type: application/json'
    );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    $result = curl_exec($ch);
    if ($result === FALSE) {
        die('Curl failed: ' . curl_error($ch));
    }
    curl_close($ch);
    return $result;
}
?>
<?php
//this block is to post message to GCM on-click
$pushStatus = "";
if ( ! empty($_GET["push"])) {
    $gcmRegID  = file_get_contents("GCMRegId.txt");
    $pushMessage = $_POST["message"];
    if (isset($gcmRegID) && isset($pushMessage)) {
        $gcmRegIds = array($gcmRegID);
        $message = array("m" => $pushMessage);
        $pushStatus = sendPushNotificationToGCM($gcmRegIds, $message);
    }
}
//this block is to receive the GCM regId from external (mobile apps)
if ( ! empty($_GET["shareRegId"])) {
    $gcmRegID  = $_POST["regId"];
    file_put_contents("GCMRegId.txt",$gcmRegID);
    echo "Ok!";
    exit;
}
?>
<html>
    <head>
        <title>Google Cloud Messaging (GCM) Server in PHP</title>
    </head>
    <body>
        <h1>Google Cloud Messaging (GCM) Server in PHP</h1>
        <form method="post"   action="gcm.php/?push=1">
            <div>
                <textarea rows="2" name="message" cols="23" placeholder="Message to transmit via   GCM"></textarea>
            </div>
            <div><input type="submit"  value="Send Push Notification via GCM" /></div>
        </form>
        <p><h3><?php echo $pushStatus; ?></h3></p>
    </body>
</html>

请告诉我,我怎么存储多个设备注册的ID在GCMRegid.txt并发送通知到每个登记的设备

Please tell me how do I store multiple device registration ids in GCMRegid.txt and send notifications to each of the registered device

推荐答案

在这里,我用mysql,而不是从检索文件中的按键改写了PHP。在这种情况下,我检索所有的 regIds 从表中,并把它们放入数组,并把它传递给sendPushNotification功能的消息推送到所有。在这里你有2个文件,一个用于连接到数据库,一个用于GCM:

Here I rewrote the php using mysql rather than retrieving the keys from file. In this case, I retrieve all the regIds from the table and put them in an array and pass it to sendPushNotification function to push the message to all. here you have 2 files, one for connect to database and one for GCM:

将connect.php:

<?php

    $db_host = "localhost";
    $db_user = "root"; //change to your database username, it is root by default
    $db_pass = '';     //change to your database password, for XAMPP it is nothing for WAMPP it is root
    $db_db = 'gcmFirst'; //change to your database name

    if(!@mysql_connect($db_host, $db_user, $db_pass) || !@mysql_select_db($db_db)) {
        die('couldnt connect to database ' .mysql_error());
    }

?>

gcm.php

gcm.php

<?php
require 'connect.php';

function sendPushNotification($registration_ids, $message) {

    $url = 'https://android.googleapis.com/gcm/send';
    $fields = array(
        'registration_ids' => $registration_ids,
        'data' => $message,
    );

    define('GOOGLE_API_KEY', 'AIzaSyCjctNK2valabAWL7rWUTcoRA-UAXI_3ro');

    $headers = array(
        'Authorization:key=' . GOOGLE_API_KEY,
        'Content-Type: application/json'
    );
    echo json_encode($fields);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    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;

}

$pushStatus = '';

if(!empty($_GET['push'])) {

    $query = "SELECT regId FROM users";
    if($query_run = mysql_query($query)) {

        $gcmRegIds = array();
        while($query_row = mysql_fetch_assoc($query_run)) {

            array_push($gcmRegIds, $query_row['regId']);

        }

    }
    $pushMessage = $_POST['message'];
    if(isset($gcmRegIds) && isset($pushMessage)) {

        $message = array('message' => $pushMessage);
        $pushStatus = sendPushNotification($gcmRegIds, $message);

    }
}

if(!empty($_GET['shareRegId'])) {

    $gcmRegId = $_POST['regId'];
    $query = "INSERT INTO users VALUES ('', '$gcmRegId')";
    if($query_run = mysql_query($query)) {
        echo 'OK';
        exit;
    }
}
?>

<html>
    <head>
        <title>Google Cloud Messaging (GCM) Server in PHP</title>
    </head>
    <body>
    <h1>Google Cloud Messaging (GCM) Server in PHP</h1>
    <form method = 'POST' action = 'gcm.php/?push=1'>
        <div>
            <textarea rows = 2 name = "message" cols = 23 placeholder = 'Messages to Transmit via GCM'></textarea>
        </div>
        <div>
            <input type = 'submit' value = 'Send Push Notification via GCM'>
        </div>
        <p><h3><?php echo $pushStatus ?></h3></p>
    </form>
    </body>
</html>

所有你需要做的就是创建一个具有用户表的id,REGID和名称列的数据库。

all you have to do is create a database that has a users table with id, regId and name as columns.

希望这是你在找什么

这篇关于发送推送通知使用GCM多台Android设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 01:35