user2661416
user2661416

Reputation: 29

GCM PHP to send all devices

I have a problem with GCM and need some help with this topic. Send a message to all mobile devices

PHP:

    $resultcoment = mysql_query("SELECT * FROM notificaciones");

    while ($row = mysql_fetch_array($resultcoment)){
    $result = '"' . $row['regId'] . '", ';
    //print_r($result);
    $registrationIDs = array($result);

}

    $message = $_POST['message'];

    $fields = array(
                'registration_ids'  => $registrationIDs,
                'data'              => array( "message" => $message ),
                );

RESULT:

"APA91bHmIHSNCSG_YVHYZMPRrXmw-E_iy7bPA9bo934n67afw8hXJHZvm1MNhrPaRtN6XbkXVjZ9T9YFH9qNi0zM5b9cPGxDhrkhysTXyRwTRLd02lQ-   v_e4zWkiGGhQt4BpH_ZLNGGAAU6hWo8ny8Mm2_d7GlEkwcKkZdNhWFO5HXGHWF4gbzM", "APA91bHmIHSNCSG_YVHYZMPRrXmw-E_iy7bPA9bo934n67afw8hXJHZvm1MNhrPaRtN6XbkXVjZ9T9YFH9qNi0zM5b9cPGxDhrkhysTXyRwTRLd02lQ-v_e4zWkiGGhQt4BpH_ZLNGGAAU6hWo8ny8Mm2_d7GlEkwcKkZdNhWFO5HXGHWF4gbzM", "APA91bElWWgUTnoI3YRRunJ_BbaAcdc4PASCB3LxcFWQM9RALQE0hc0c1xVF-EIR7iLYFlbeMDjnMPY-503fqzOPAjJT514zo1j86uhUobhlu79PKtvzjMXiBavoiUcaSKY__JVTbQALvmZ8NJtzzRG81Tf1-svTjw", "APA91bElWWgUTnoI3YRRunJ_BbaAcdc4PASCB3LxcFWQM9RALQE0hc0c1xVF-EIR7iLYFlbeMDjnMPY-503fqzOPAjJT514zo1j86uhUobhlu79PKtvzjMXiBavoiUcaSKY__JVTbQALvmZ8NJtzzRG81Tf1-svTjw", 

    {"multicast_id":8410297698153738741,"success":0,"failure":1,"canonical_ids":0,"results":   [{"error":"InvalidRegistration"}]}

What's the problem?

Upvotes: 1

Views: 1261

Answers (1)

Loic Sharma
Loic Sharma

Reputation: 32

You're overwriting the array in each iteration. You want to push the elements to the end of the registrationIds array. Here's how:

$resultcoment = mysql_query("SELECT * FROM notificaciones");
$registrationIDs = array();

while ($row = mysql_fetch_array($resultcoment)){
  $result = '"' . $row['regId'] . '", ';
  //print_r($result);
  $registrationIDs[] = $result;
}

$message = $_POST['message'];

$fields = array(
            'registration_ids'  => $registrationIDs,
            'data'              => array( "message" => $message ),
            );

Upvotes: 0

Related Questions