使用PHP向Android设备发送的FCM通知不会始终显示在设备上

问题描述

我有一个用户设备注册ID的数据库,并试图一次向所有人发送通知。 ID的总数约为100。每当我发送带有单个设备ID(to)的测试通知时,该通知就会显示在设备上。但是,每当我将其批量发送(registration_ids)时,通知就会在某些设备上显示。我在下面附上了我的编码。请帮助我解决问题

function fcm_notif_divide_send($reg_id,$total,$message) {
$options = get_option('fcm_setting');

$data = array( 'registration_ids' => null,'to' => null,'data' => $message );

if($reg_id != ""){
    $data['to'] = $reg_id;
    $push_response = fcm_notif_send($data);
} else if ($options['notif-topic'] == true) {
    $data['to'] = "/topics/" . NOTIFICATION_TOPIC;
    $push_response = fcm_notif_send($data);
} else {
    $page = floor($total / 1000);
    for ($i = 0; $i <= $page; $i++){
        $regid_arr = fcm_data_get_regid_by_page(1000,($i * 1000));
        // send notification per 1000 items
        $data['registration_ids'] = $regid_arr;
        $priority = "high";
        $data['priority'] = $priority;
        $push_response = fcm_notif_send($data);
    }
}

$resp = array('status' => 'SUCCESS','msg' => 'Notification sent successfully');
if ($reg_id != "" && isset($push_response['results'][0]['error'])){
    $resp['msg'] = $push_response['results'][0]['error'];
    $resp['status'] = 'SUCCESS';
}
return $resp;
}

function fcm_notif_send($data) {
$error = false;

//Get Option
$fcm_api_key=get_option('fcm_setting')['fcm-api-key'];
if(empty($fcm_api_key) || strlen($fcm_api_key) <= 0) {
    $error = true;
    return $error;
}

$url = 'https://fcm.googleapis.com/fcm/send';

$headers = array( 'Authorization: key=' . $fcm_api_key,'Content-Type: application/json' );
// Open connection
$ch = curl_init();

// Set the url,number of POST vars,POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

// disabling SSL Certificate support temporary
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($data));

// Execute post
$response = curl_exec($ch);
curl_close($ch);

return json_decode($response,true);
}

?>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)