[英]php script executing twice when i am sending fcm notifications using curl
I am trying to send notification to multiple users using FCM with a curl function in PHP. 我正在尝试使用FCM和PHP中的curl函数向多个用户发送通知。 Sometimes it is sending perfectly, sometimes it is re-executing entire page and inserting values twice and sending notification twice.
有时它发送完美,有时它重新执行整个页面并两次插入值并两次发送通知。
Here is my notification code 这是我的通知代码
$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;
}
I had same problem, I solved it by suing the following code: 我有同样的问题,我通过使用以下代码解决了它:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.