I want to send notifications through firebase to users (Web and Mobile) as i am developing a progressive web app.
I have created a project in Firebase by following their tutorial. Everything is working well, and also i tested sending notification using Curl and it works on Computer and Mobile both properly.
curl --header "Content-Type: application/json" \
--header "Authorization: key=<MY-FIREBASE-SERVER-KEY>" \
-d '{
"notification": {
"title": "New Message",
"body": "Hello, How are you ?",
"icon": "/images/profile_placeholder.png",
"click_action": "http://localhost:5000"
},
"to": "MY-CLIENT-FCM-TOKEN"
}' \
https://fcm.googleapis.com/fcm/send
It works properly and shows the notification.
But when I use PHP for sending notification then it sends the notification but with the message as "this site has been updated in the background".
Here is the php file: pwa_demo2.php
<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', '<MY-FIREBASE-SERVER-KEY>' );
// prep the bundle
$msg = array
(
'message' => 'Hello, How are you ?',
'title' => 'New Message'
);
$fields = array
(
'to' => 'MY-CLIENT-FCM-TOKEN',
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_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_VERIFYHOST, 0);
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
if($result === false)
{
die('Curl failed: '.curl_error($ch));
}
curl_close( $ch );
echo $result;
?>
So please help me as i am stuck in this for hours...
Your PHP code is not sending the same message as the CURL command line.
Try printing the output of json_encode( $fields )
and check if it's the same as the payload that you are passing to the CURL
command line with the -d
option