简体   繁体   中英

Google Cloud Messaging: Send message to “all” users

I am following this tutorial to understand the system of GCM. I have a question regarding this part:

It is possible to send messages to every single registered user, but how to change that code so I can send a single message to ALL registered devices?

I've already looked for answers:

sending push notifications to multiple android devices using GCM

and

Sending Push Notification on multiple devices

(nearly same question) - but could not find any answer solving my question.

<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>

i tried to change the code, but with my change it does not work..

I want to put all regID as array into 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>

with this I can get all regids into the array rows... but how can i transfer the array into the form?

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

my problem is how to send my array "row" to send_message.php?

$registatoin_ids = array($regId); << it does already handles array, but my input works only with one value, i want to transfer array^^

EDIT: 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;
}
?>

It's your responsibility to maintain a list of the Registration IDs of all the registered users. There's no API call to send a message to all registered users. You can send to GCM server a request with up to 1000 Registration IDs. If you have more than 1000 registered users, you'll have to split them to multiple requests.

<?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;
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM