PHP-скрипт выполняется дважды при отправке уведомлений fcm с помощью curl

Я пытаюсь отправить уведомление нескольким пользователям, использующим FCM с функцией curl в PHP. Иногда он отправляет отлично, иногда он заново выполняет всю страницу и вставляет значения дважды и отправляет уведомление дважды.

Вот мой код уведомления

$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;

}

0

Решение

У меня была та же проблема, я решил ее, подав в суд на следующий код:

define('API_ACCESS_KEY', 'ftfh');
$title = $_GET['title'];
$body = $_GET['body'];

$fields = array(
"registration_ids" => $registrationIds_array,
"notification" => array(
"body" => $body,
"title" => $title,
"icon" => $appicon,
),
"data" => array(
"isUrl" => true,
"url" => "https://www.amazon.in/",
"isMsg" => false,
"msg"=>"Hello Swapnil...")
);

$fields= json_encode($fields);

$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
#Send Reponse To FireBase Server
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, ( $msg));
curl_setopt($ch, CURLOPT_TIMEOUT, 10); //timeout in seconds

$result = curl_exec($ch);
curl_close($ch);
0

Другие решения

Других решений пока нет …