I am trying to send fcm notification to a user but, while running this code to deliver the message, it just takes forever, it neither sends nor gives me any response. Using PHP, shared server. Please help.
$msg = array
(
"title" => 'TEST',"body" => 'test',"icon" => '/setup/favicn.png'
);
$fields = array
(
'to' => 'USER_TOKEN',
'data' => $msg
);
$headers = array
(
'Authorization: key=' . 'MY SERVER KEY',
'Content-Type: application/json'
);
$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, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
print_r($result);
CodePudding user response:
$data = array(
'title'=>$title,
'sound' => "default",
'body'=>$body,
'color' => "#79bc64"
);
$fields = array(
'to'=>$to,{//device token}
'notification'=>$data,
'data'=>$data,
"priority" => "high"
);
$headers = array(
'Authorization: key=Server Key', {//after key= just paste your server key.}
'Content-Type: application/json'
);
$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, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
curl_close( $ch );
return $result;
CodePudding user response:
Thanks guys, turns out the server was unable to connect to the host fcm.googleapis.com. I checked it after doing some ping from the server. And the reason to that was port issue. FCM typically uses port 5228, but it sometimes uses 443, 5229, and 5230. In addition, IP addresses of FCM endpoint may change from time to time. Also, Firebase Cloud Messaging (FCM) doesn't provide specific IPs, so anyone having this issue should allow their firewall to accept outgoing connections to all IP addresses contained in the IP blocks listed in this file https://www.gstatic.com/ipranges/goog.json This large list is updated regularly, and we are recommended to update our rules on a monthly basis. Problems caused by firewall IP restrictions are often intermittent and difficult to diagnose.