如何在安卓设备使用 Firebase 向1000多个用户发送推送通知?

3

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

1
您可以将令牌集合分为每个子集1000个令牌,然后对于每个1000个令牌集合调用函数send_notification。确定您在数据库中拥有的令牌总数,并一次发送1000个令牌,然后继续下一个1000个令牌集合。 - Nilesh Singh
我知道我必须将消息分成1000个块发送,但是由于我对PHP的了解非常有限,我该如何实现它呢? - Arbaz Alam
请问您需要什么样的帮助? - Nilesh Singh
我跟着你的代码走,结果显示成功,但是我没有收到任何PUSH消息。 - techDigi
@techDigi 请点击此链接 https://github.com/miskoajkula/Fcm 获取详细信息。 - Arbaz Alam
显示剩余3条评论
1个回答

12

你可以使用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;
请在您的代码中进行更改。

请告诉我$RegId和$gcm_regid来自哪里,它们的定义是什么? - Arbaz Alam
请检查tokens数组,我错误地将它们打印出来了。 - Jydipsinh Parmar
@JDTheRealHero,我尝试了你的代码,但是出现了以下错误:"registration_ids"字段不是JSON数组。 - Mahdi H
请查看此链接:http://myphpinformation.blogspot.in/2017/07/create-custom-notification-in-andoid-using-fcm.html。 - Jydipsinh Parmar
https://stackoverflow.com/a/52968379/1318946 - Pratik Butani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接