gpt4 book ai didi

php - 修复 GCM 1000 条消息限制,帮助循环脚本

转载 作者:太空狗 更新时间:2023-10-29 14:47:26 25 4
gpt4 key购买 nike

我正在使用 PHP 向用户发送 GCM 通知。基于他们在服务器上的 SQL 中保存的 token 。但是我一次只能发送 1000 个通知,

这是我的工作代码,现在我需要对其进行编辑以循环处理该过程,并分段发送 5000 个通知,1000 个比 1000 个等。

这是我的 PHP 代码,以及我没有在此处发布的简单 HTML,

        <?php

$mysqlHost = "";
$mysqlUser = "";
$mysqlPwd = ";
$mysqlDbname = "";


class GCM {
function __construct(){}

public function send_notification($registatoin_ids,$data) {

// GOOGLE API KEY
define("GOOGLE_API_KEY","************");
$url="https://android.googleapis.com/gcm/send";
$fields=array(
"registration_ids"=>$registatoin_ids,
"data"=>$data,
);
//var_dump($fields);
$headers=array(
"Authorization: key=".GOOGLE_API_KEY,
"Content-Type: application/json"
);
$ch=curl_init();
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_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($fields));
$result_gcm=curl_exec($ch);
if($result_gcm===FALSE){
die("Curl failed: ".curl_error($ch));
}
curl_close($ch);
//echo $result_gcm;
}
}



// Create connection
$conn = mysqli_connect($mysqlHost, $mysqlUser, $mysqlPwd, $mysqlDbname);

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}


$result = $conn->query("SELECT * FROM users WHERE users_android_token IS NOT NULL AND users_android_token <> ''");

$android_tokens = array();
$x=0;

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$android_tokens[] = $row["users_android_token"];
$x++;
}
} else {
echo "0 results";
}
$conn->close();

$title = $_POST['title'];
$msg = $_POST['message'];
$link = $_POST['link'];

if ($android_tokens != array()) {
$gcm=new GCM();
$data=array("title"=>$title,"description"=>$msg,"link"=>$link);
$result_android=$gcm->send_notification($android_tokens,$data);
}
?>

最佳答案

为什么不使用按主题发送通知,而不是通过 registrationId 发送通知。您只需要让客户订阅某个主题即可。

在 GCM 中,您可以使用以下代码发送通知主题:

function send_notification_topic($topicname, $message){
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'to' => '/topics/' . $topicname,
'data' => $message
);

$headers = array(
'Authorization: key=AIzablablablayourServerKey',
'Content-Type: application/json'
);

$ch = curl_init();
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_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

$result = curl_exec($ch);
if($result === FALSE){
die('Curl failed : ' . curl_error($ch));
}
curl_close($ch);
return $result;
}

关于php - 修复 GCM 1000 条消息限制,帮助循环脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38368540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com