我正在开发一个应用程序,我正处于最后的面孔,数据库中的注册设备将一次接收推送通知。
目前,使用我的脚本,它只能发送到一个设备。我的数据库中有一个名为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);
?>
答案 0 :(得分:1)
从数据库中获取所有设备ID,如下所示:
$registrationIDs = array();
$query = "select device_id from devices";
while($row = $db->database_fetch_assoc($query))
{
$registrationIDs[] = $row;
}
现在将此数组传递给$field
数组。
答案 1 :(得分:0)
创建您的查询以在$ registrationID中获取设备ID并将所有内容发送到服务器。
// Replace with the real client registration IDs
$registrationIDs = array("APA91bHhTiGeAKgphENoGH6********Blw9PspLCPAiprTSDN6mRuW3k253PZyppxgJaqPftTzYZOiWPt5C4XFVmkuqjbXp7MV3mAmpDAYM-11LdIdU0");