从 mysql 数据库一次向多个用户发送 gcm


Sending gcm to multiple users at a time from mysql database

我正在开发一个应用程序,我正在最后一张脸上,数据库中的注册设备一次要接收推送通知。

目前,使用我拥有的脚本,它只能发送到一台设备。我的数据库中有一个名为 devices 的表,保存设备 ID 的字段称为 device_id。请提供有关如何发送给多个用户的任何帮助?

<?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);
?>

从数据库中获取所有设备 ID,如下所示:

$registrationIDs = array();
$query = "select device_id from devices";
while($row = $db->database_fetch_assoc($query))
{
    $registrationIDs[] = $row;
}

现在将此数组传递给$field数组。

创建查询以获取$registrationIDs中的设备 ID 并将其全部发送到服务器。

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