D4rWiNS
D4rWiNS

Reputation: 2615

Sending push notification to multiple users android

i am trying yo send a push notification to multiple users, when i do it for 1 user it work perfectly the problem come when i try to send the message to multiple users.

I got the response 200 MissingRegistration (So i supposed the problem is that i dont send the ID on the correct way)

Both ids are correct because if i send the message to both individually it works

This is my code

    $gcmcodes=array("one user key","the other user key");

    if(isset($_GET['mensaje']))    $message = $_GET['mensaje'];
    $message2 = new gcm(); 
    $message2->sendMessageToPhone(2, $message,$gcmcodes);

  function sendMessageToPhone($collapseKey, $messageText, $gcmcodes)
  {

$apiKey = 'My apikey';

$headers = array('Authorization:key=' . $apiKey);
$data = array(
  'registration_ids' => $gcmcodes,
  'collapse_key' => $collapseKey,
  'data.message' => $messageText);

$ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send");
if ($headers)
 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo"<pre>";
var_dump($gcmcode,$response);
echo"</pre>";

if (curl_errno($ch)) {
 return 'fail';
}
if ($httpCode != 200) {
 return 'status code 200';
}
curl_close($ch);
return "mensaje mandado con exito";

}

Upvotes: 3

Views: 3255

Answers (1)

Eran
Eran

Reputation: 393821

The format you are using can only be used for sending a single notification at a time. In order to send the notification to multiple registration IDs, you have to send a JSON request of the form :

{ "collapse_key": "something",
  "time_to_live": 108,
  "delay_while_idle": true,
  "data": {
    "message": "some message"
  },
  "registration_ids":["4", "8", "15", "16", "23", "42"]
}

Oh, and the reason you got MissingRegistration error is that when you use the plain text format, GCM expects a single Registration ID to be the value of the registration_id key (not registration_ids as you tried).

Upvotes: 3

Related Questions