如何在Android中使用firebase向超过1000个用户发送推送通知


How to send push notification to more than 1000 users using firebase in Android?

当向超过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);
?>

Hello你可以使用array_chunk发送1000个通知

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;