Reputation: 591
I implementing GCM for group messaging. My situation:
Referring to https://developers.google.com/cloud-messaging/notifications, I can send notification to GROUP 1, but it is impossible to send to both group (GROUP 1 and 2).
Here's my code:
<?php
define( 'API_ACCESS_KEY', 'MyAPI' );
$notification_key = 'notificationKeyHere';
// prep the bundle
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title',
'subtitle' => 'This is a subtitle. subtitle',
'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);
$fields = array
(
'to' => $notification_key,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result; ?>
Code above works for sending to only one group. So then later I tried:
$notification_key = array("nKey1", "nKey2");
The error produced:
Field "to" must be a JSON string: ["nKey1", "nKey2"]
So how can I alter my code so that both group can received the notification?
Upvotes: 0
Views: 406
Reputation: 4260
If you are sending messages to a single device use 'to'
'to' => $notification_key,
'data' => $msg
But if you are sending messages to multiple devices use 'registration_ids',
$ids = array("nKey1", "nKey2");
and send as
'registration_ids' => $ids,
'data' => $msg,
Upvotes: 0