How to send push notification to more than 1000 users using firebase in Android?(如何在 Android 中使用 firebase 向 1000 多个用户发送推送通知?)
问题描述
向超过 1000 个用户发送推送通知时,我收到以下错误消息:
I am getting following error message when sending push notification to more than 1000 users:
firebase 批量消息数 (1082) 超过允许的最大值 (1000)"
"firebase Number of messages on bulk (1082) exceeds maximum allowed (1000)"
我在谷歌上搜索了这个问题,发现 FCM 每个请求只能发送 1000 条消息.解决方法:在php中拆分发件人列表,可惜我是php新手,无法实现循环.
I have googled the issue and found that FCM can send only 1000 message per request. Solution: Split sender list in php, but unfortunately I am new to php and unable to achieve the looping.
我的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 脚本来拆分用户.
Can you please help me in this regard and provide me a push_notification.php script that will split the users.
我试图将消息分成 1000 条,但没有成功:
I have tried to split the message in chunks of 1000 but get no success:
<?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);
?>
推荐答案
您好,您可以使用 array_chunk 发送 1000 个通知
Hello you can use array_chunk for send 1000 notification
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.");
$regIdChunk=array_chunk($tokens,1000);
foreach($regIdChunk as $RegId){
$message_status = send_notification($RegId, $message);
}
echo $message_status;
请在您的代码中更改这一点.
Please change this in your code.
这篇关于如何在 Android 中使用 firebase 向 1000 多个用户发送推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Android 中使用 firebase 向 1000 多个用户发送推送通知?
基础教程推荐
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01