user5915278
user5915278

Reputation: 25

Sending gcm to multiple users at a time from mysql database

I'm developing an app and I'm on the final face where registered devices in the database are to receive push notifications at a time.

Currently, with the script I have, it can only send to one device. I have a table in my database called devices and the field that saves the device ID is called device_id. Please any help on how to send to multiple users?

<?php

$message = $_POST['title'];
$id = $_POST['id'];

// Replace with the real server API key from Google APIs
$apiKey = "AIzaSyDn9RPYmnlbs9*****WHKksz73klOqxM";

// Replace with the real client registration IDs
$registrationIDs = array("APA91bHhTiGeAKgphENoGH6********Blw9PspLCPAiprTSDN6mRuW3k253PZyppxgJaqPftTzYZOiWPt5C4XFVmkuqjbXp7MV3mAmpDAYM-11LdIdU0");

// Message to be sent

// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';

$fields = array(
  'registration_ids' => $registrationIDs,
  'data' => array( "message" => $message,"title" => "Today's Devotion","id" => $id),
);
$headers = array(
  'Authorization: key=' . $apiKey,
  'Content-Type: application/json'
);

// Open connection
$ch = curl_init();

// Set the URL, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_POST, true);
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields));

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields));

// Execute post
$result = curl_exec($ch);

// Close connection
curl_close($ch);
//echo $result;
//print_r($result);
//var_dump($result);
?>

Upvotes: 0

Views: 182

Answers (2)

Prerak Sola
Prerak Sola

Reputation: 10009

Fetch all the device ids from your database as below:

$registrationIDs = array();

$query = "select device_id from devices";

while($row = $db->database_fetch_assoc($query))
{
    $registrationIDs[] = $row;
}

Now pass this array to the $field array.

Upvotes: 1

Hunain
Hunain

Reputation: 396

create your query to get device ids in $registrationIDs and send all to server.

// Replace with the real client registration IDs
    $registrationIDs = array("APA91bHhTiGeAKgphENoGH6********Blw9PspLCPAiprTSDN6mRuW3k253PZyppxgJaqPftTzYZOiWPt5C4XFVmkuqjbXp7MV3mAmpDAYM-11LdIdU0");

Upvotes: 0

Related Questions