Hello, I am working on a project where I have to send notifications to registered devices, I have written a complete code for that but firebase not sending push notifications to any device i.e android neither IOS. I am attaching my code please have a look at it.
if(!$device_tokens->isEmpty()){
return sendPushNotification($device_tokens, "Confirmation alert", "Have you got the room?",1);
}
function sendPushNotification($registration_ids,$title, $message, $id=NULL)
{
$header = [
'authorization:key' . env('FCM_KEY'),
'content-type: application/json'
];
$url = env('FCM_URL');
$post_data = '{
"registration_ids" : "' . $registration_ids . '",
"data" : {
"title" : "' . $title . '",
"description" : "' . $message . '",
"id" : "' . $id . '",
"text" : "' . $message . '",
},
"notification" : {
"body" : "' . $message . '",
"title" : "' . $title . '",
"description" : "' . $message . '",
"sound" : "default"
}
}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
if ($result === false) {
$result_noti = 0;
} else {
$result_noti = 1;
}
return $result_noti;
//curl_close($ch);
}
I have checked the device token variable and it contains device tokens that have to be sent in the form: ['deivcetoken1','devicetoken2']. I also dd server key and URL and they are also fine, Even curl request returns true response but push notification still not received on the android/ios device.
P.S: My FCM_URL is:https://fcm.googleapis.com/fcm/send and Firebase server key is stored in env file as FCM_KEY, Thanks.