I'm trying to send a clickable push message through PHP with FCM. Sending a notification with an image, a title and a body works but it doesn't work for links.
I'm using PHP & cURL to send a JSON to https://fcm.googleapis.com/fcm/send The function I'm using is one found here.
function sendnotification($to, $title, $message, $img = "", $datapayload = "")
{
$msg = urlencode($message);
$data = array(
'title'=>$title,
'sound' => "default",
'msg'=>$msg,
'data'=>$datapayload,
'body'=>$message,
'color' => "#FF33CC"
'link' => "https://www.mylink.com"
);
if($img)
{
$data["image"] = $img;
$data["style"] = "picture";
$data["picture"] = $img;
}
$fields = array(
'to'=>$to,
'notification'=>$data,
"priority" => "high",
"link"=>"https://www.mylink.com"
);
$headers = array(
'Authorization: key= MYKEY',
'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;
}
With this, I get the notification with the right picture, the right text but when I click on it, the notification doesn't lead me to the specified link.
The documentation mentions a totally different format for the JSON:
{
"message": {
"token": "eEz-Q2sG8nQ:APA91bHJQRT0JJ...",
"notification": {
"title": "Background Message Title",
"body": "Background message body"
},
"webpush": {
"fcm_options": {
"link": "https://dummypage.com"
}
}
} }
I have tried using this type of JSON but when I do, I don't get any notification, I don't even get a respons from my cURL call.
My service working is also pretty standard:
importScripts("https://www.gstatic.com/firebasejs/9.6.1/firebase-app-compat.js");
importScripts("https://www.gstatic.com/firebasejs/9.6.1/firebase-messaging-compat.js");
importScripts("https://www.gstatic.com/firebasejs/9.6.1/firebase-analytics-compat.js");
firebase.initializeApp({
messagingSenderId: "653804269445",
apiKey: "AIzaSyC9cNi-pXCshr7u7RPBRezP0UBwLSUoXUs",
projectId: "blognotif-b0f91",
appId: "1:653804269445:web:5db4034605e19e6b81deec",
});
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function (payload) {
console.log('Handling background message ', payload);
return self.registration.showNotification(payload.data.title, {
body: payload.data.body,
icon: payload.data.icon,
tag: payload.data.tag,
data: payload.data.link,
});
});
self.addEventListener('notificationclick', function (event) {
event.notification.close();
event.waitUntil(self.clients.openWindow(event.notification.data));
});
I have also tried to replace data: by link: but it doesn't help.
I get the following error message in Chrome (not in Firefox) : Uncaught TypeError: messaging.setBackgroundMessageHandler is not a function
I ready here it was because I use "notification" in the JSON but I have not found a working alternative.
Which JSON format should I use to make my notifications clickable?
thanks!