我正在尝试使用 FCM 和 PHP 中的 curl 函数向多个用户发送通知。有时它发送完美,有时它重新执行整个页面并插入两次值并发送两次通知。
这是我的通知代码
$response = sendNotification(
$apiKey,
$reg_id1,
array(
'message' => $name,
'title' => $serviceName,
'subtitle' => $serviceName,
'NotificationType' => 'Notify',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
)
);
echo $response;
function sendNotification( $apiKey, $registrationIdsArray, $messageData ){
$headers = array("Content-Type: application/json", "Authorization: key=" . $apiKey);
$data = array(
'registration_ids' => $registrationIdsArray,
'data' => array('data' => $messageData)
);
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_URL, "https://fcm.googleapis.com/fcm/send" );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data) );
$response = curl_exec($ch);
curl_close($ch);
return $response;
}