【发布时间】:2016-12-27 07:02:42
【问题描述】:
向超过 1000 个用户发送推送通知时,我收到以下错误消息:
“firebase 批量消息数 (1082) 超过允许的最大值 (1000)”
我搜索了这个问题,发现 FCM 每个请求只能发送 1000 条消息。 解决方法:在php中拆分发件人列表,可惜我是php新手,无法实现循环。
我的php脚本是:
<?php
function send_notification ($tokens, $message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $tokens,
'data' => $message,
'click_action' => ACTIVITY_CIRCULAR
);
$headers = array(
'Authorization:key = <MY_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_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;
}
$conn = mysqli_connect("localhost","username","password","databse_name");
$sql = " Select Token From users";
$result = mysqli_query($conn,$sql);
$tokens = array();
if(mysqli_num_rows($result) > 0 ){
while ($row = mysqli_fetch_assoc($result)) {
$tokens[] = $row["Token"];
}
}
mysqli_close($conn);
$message = array("message" => "Hi This Arbaz Alam And You are using Android App Thanks.");
$message_status = send_notification($tokens, $message);
echo $message_status;
?>
您能否在这方面帮助我,并提供一个 push_notification.php 脚本来拆分用户。
我尝试将消息分成 1000 条,但没有成功:
<?php
function send_notification ($tokens, $message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $tokens,
'data' => $message
);
$headers = array(
'Authorization:key = <MY-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_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;
}
function send_message($tokens)
{
$message = array("message" => "AKTU Even Semester Result is updated");
$message_status = send_notification($tokens, $message);
echo $message_status;
}
$conn = mysqli_connect("localhost","username","password","database_name");
$sql = " Select Token From users";
$result = mysqli_query($conn,$sql);
$tokens = array();
$num_of_rows = mysqli_num_rows($result);
echo "$num_of_rows";
if(mysqli_num_rows($result) > 0 ){
while ($row = mysqli_fetch_assoc($result)) {
$tokens[] = $row["Token"];
if(count($tokens) == 1000){
send_message($tokens);
$tokens = array();
}
}
}
send_message($tokens);
mysqli_close($conn);
?>
【问题讨论】:
-
您可以将一组令牌划分为每个 1000 个令牌的子集,然后可以为每 1000 个令牌集调用函数 send_notification。确定数据库中的令牌总数并立即发送到 1000 个,然后移至下一组 1000 个,依此类推。
-
我知道我必须以 1000 条为单位发送消息,但由于我对 php 知之甚少,我该如何实现它。
-
能否请您说明您需要什么样的帮助?
-
@Nilesh 我在评论中添加了新代码,但没有成功。
-
我遵循您的代码,结果显示成功,但我没有收到任何推送消息..
标签: php android firebase push-notification firebase-cloud-messaging