java - 谷歌云消息 : Send message to "all" users

标签 java php android google-cloud-messaging

我正在关注 this tutorial了解GCM的系统。我对这部分有疑问:

可以向每个注册用户发送消息,但如何更改该代码以便我可以向所有注册设备发送一条消息?

我已经在寻找答案了:

sending push notifications to multiple android devices using GCM

Sending Push Notification on multiple devices

(几乎相同的问题)- 但找不到解决我的问题的任何答案。

<body>
    <?php
    include_once 'db_functions.php';
    $db = new DB_Functions();
    $users = $db->getAllUsers();
    if ($users != false)
        $no_of_users = mysql_num_rows($users);
    else
        $no_of_users = 0;
    ?>
    <div class="container">
        <h1>No of Devices Registered: <?php echo $no_of_users; ?></h1>
        <hr/>
        <ul class="devices">
            <?php
            if ($no_of_users > 0) {
                ?>
                <?php
                while ($row = mysql_fetch_array($users)) {
                    ?>
                    <li>
                        <form id="<?php echo $row["id"] ?>" name="" method="post" onsubmit="return sendPushNotification('<?php echo $row["id"] ?>')">
                            <label>Name: </label> <span><?php echo $row["name"] ?></span>
                            <div class="clear"></div>
                            <label>Email:</label> <span><?php echo $row["email"] ?></span>
                            <div class="clear"></div>
                            <div class="send_container">                                
                                <textarea rows="3" name="message" cols="25" class="txt_message" placeholder="Type message here"></textarea>
                                <input type="hidden" name="regId" value="<?php echo $row["gcm_regid"] ?>"/>
                                <input type="submit" class="send_btn" value="Send" onclick=""/>
                            </div>
                        </form>
                    </li>
                <?php }
            } else { ?> 
                <li>
                    No Users Registered Yet!
                </li>
            <?php } ?>
        </ul>
    </div>
</body>

我试图更改代码,但我的更改不起作用..

我想将所有 regID 作为数组放入 sendPushNofiy...

<body>
    <?php
    include_once 'db_functions.php';
    $db = new DB_Functions();
    $users = $db->getAllUsers();
    if ($users != false)
        $no_of_users = mysql_num_rows($users);
    else
        $no_of_users = 0;
    ?>
    <div class="container">
        <h1>No of Devices Registered: <?php echo $no_of_users; ?></h1>
        <hr/>
        <ul class="devices">
            <?php
            if ($no_of_users > 0) {
                ?>
                <?php
                    <li>      
                        $rows = array();
                        while(($row = mysql_fetch_array($users))) {
                            $rows[] = $row['id'];
                        }
                        <form id="<?php echo $rows ?>" name="" method="post" onsubmit="return sendPushNotification('<?php echo $rows ?>')">
                            <label>Name: </label> <span><?php echo $row["name"] ?></span>
                            <div class="clear"></div>
                            <label>Email:</label> <span><?php echo $row["email"] ?></span>
                            <div class="clear"></div>
                            <div class="send_container">                                
                                <textarea rows="3" name="message" cols="25" class="txt_message" placeholder="Type message here"></textarea>
                                <input type="hidden" name="regId" value="<?php echo $row["gcm_regid"] ?>"/>
                                <input type="submit" class="send_btn" value="Send" onclick=""/>
                            </div>
                        </form>
                    </li>
                    <?php 
            } else { ?> 
                <li>
                    No Users Registered Yet!
                </li>
            <?php } ?>
        </ul>
    </div>


</body>

有了这个,我可以将所有 regids 放入数组行中......但是我如何将数组传输到表单中?

while ($row = mysql_fetch_array($users)) {
    $rows[] = $row["gcm_regid"];
}

我的问题是如何将我的数组“行”发送到 send_message.php?

$registatoin_ids = array($regId); << 它确实已经处理了数组,但我的输入只适用于一个值,我想传输数组^^

编辑: 发送消息.php

<?php

if (isset($_GET["regId"]) && isset($_GET["message"])) {
    $regId = $_GET["regId"];
    $message = $_GET["message"];
    
    include_once './GCM.php';
    
    $gcm = new GCM();

    $registatoin_ids = array($regId);
    $message = array("price" => $message);

    $result = $gcm->send_notification($registatoin_ids, $message);

    echo $result;
}
?>

最佳答案

您有责任维护所有注册用户的注册 ID 列表。没有向所有注册用户发送消息的 API 调用。您可以向 GCM 服务器发送最多包含 1000 个注册 ID 的请求。如果您有 1000 多个注册用户,则必须将他们分成多个请求。

关于java - 谷歌云消息 : Send message to "all" users,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27202601/

相关文章:

java - 不使用 wait 关键字同步块(synchronized block)是否安全?

java - Blackberry:读取项目中打包的文本文件(更快)

PHP 对空值的位置进行排序但保持结构

java - 使用选择排序方法对二维数组进行排序并将结果写入文件

android - Android Studio 3.0 canary 2 中带有 Android DataBinding 的 Kotlin

java - 当它最初位于循环内部时,返回循环外部的值

java - DownloadManager 无法在 Android Q 上运行 - Android/Java

PHP - 在函数内设置 session

android - MediaStore.Images.Thumbnails.getThumbnail 中的原始 ID 是什么?

android - 设置位置管理器 RequestLocationUpdate