簡體   English   中英

Google Cloud Messaging:向“所有”用戶發送消息

[英]Google Cloud Messaging: Send message to “all” users

我正在按照本教程了解 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>

我試圖更改代碼,但我的更改不起作用..

我想將所有 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); << 它已經處理了數組,但我的輸入只適用於一個值,我想傳輸數組^^

編輯:send_message.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 多個注冊用戶,則必須將它們拆分為多個請求。

<?php
if (isset($_GET["message"])) {
  include_once 'db_functions.php';
  $db = new DB_Functions();
  $users = $db->getAllUsers();
  $regIds = array();
  if ($users != false){
    while ($row = mysqli_fetch_array($users)) {
        $regIds[] = $row["gcm_regid"];
    }
  }

  include_once './GCM.php';

  $gcm = new GCM();

  $registatoin_ids = $regIds;
  $message = "array("price" => $_GET["message"])";

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

  echo $result;
}
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM