Google云消息传递:向"所有"用户发送消息

Ste*_*eve 2 php java android google-cloud-messaging

我正在学习本教程以了解GCM系统.我对这部分有疑问:

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

我已经找到了答案:

使用GCM向多个Android设备发送推送通知

在多个设备上发送推送通知

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

<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>
Run Code Online (Sandbox Code Playgroud)

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

我想把所有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>
Run Code Online (Sandbox Code Playgroud)

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

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

          }
Run Code Online (Sandbox Code Playgroud)

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

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

编辑:send_message.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;
}
?>
Run Code Online (Sandbox Code Playgroud)

Era*_*ran 6

您有责任维护所有注册用户的注册ID列表.没有API调用来向所有注册用户发送消息.您可以向GCM服务器发送最多1000个注册ID的请求.如果您有超过1000个注册用户,则必须将它们拆分为多个请求.