3

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;
}
?>
yyppodq
  • 67
  • 3
Steve
  • 127
  • 1
  • 10
  • Oyur code already knows how to send a message to one device (see your `onsubmit="return sendPushNotification(...`). The code behind this sendPushNotification method has to be executed for every registration id. I recommend to not do it in the JavaSript. Move the send loop to the PHP code, and as Eran explained do not send more than 1000 messages in one request. – mjn Nov 29 '14 at 13:13
  • yeah i know, it already sends one message to one device - but I want to send only ONE message to ALL devices registred^^... i think it's only some change of php code to handle that - – Steve Nov 29 '14 at 13:20
  • The Google server does not know which devices are registered with your back-end server, so you will always have to send individual messages, one per device, with identical content – mjn Nov 29 '14 at 17:05
  • yes I know, I think I am missunderstood: What the code from: http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/ is doing is: looping threw all the user id's to show a form, for sending a message for every single regID. i want to change that formular, that the formular does not get a single regID - it shall get an array of regID (all regID in table) - but i could not make that. See my Edit there is a send_message.php , that already can handles the regID as array - but the regID is still no array - – Steve Nov 29 '14 at 18:19

2 Answers2

6

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.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Hmm ok that's an aspect i did not thought about... ok but when I can split it, then there is no problem.. :) – Steve Nov 29 '14 at 13:09
2
<?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;
}
?>
Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
Viv
  • 1,706
  • 1
  • 18
  • 27