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

Sending gcm to multiple users at a time from mysql database

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

目前我手上的脚本只能发送到一台设备。我的数据库中有一个名为 devices 的 table,保存设备 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);
?>

创建您的查询以在 $registrationIDs 中获取设备 ID 并将所有内容发送到服务器。

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

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

$registrationIDs = array();

$query = "select device_id from devices";

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

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