I'm currently working on integrating Firebase Cloud Messaging (FCM) for push notifications in my web application. I have a question regarding the behavior of clicks on web-push notifications and how to define this behavior and gather information and analytics.
My question is some what similar to these questions:
- click_action attribute for web push notification through FCM
- Missing click-action key for webpush payload on message sent via FCM HTTPv1
- HTTP v1 API: "click_action" for webpush notification
However, there are some differences.
The Firebase documentation doesn't explicitly address whether to use click_action or service worker to handle click behavior.
I have read the Message Types article, which mentions two types of messages:
- Notification messages
- Data messages
But I didn't understand the difference between them.
Let's say I want end-users who receive notifications from my backend systems to be directed to example.com when they click on the notification.
I've achieved this using a service worker:
self.addEventListener("notificationclick", (event) => {
const notificationData = event.notification.data;
const targetPageUrl = notificationData && notificationData.tracking
return clients.openWindow(targetPageUrl);
}
As you can see, it parses the data field in the notification payload and then opens the URL.
Here's the payload I'm using:
"webpush": {
"notification": {
"title": "This is a test notification",
"body": "test test test",
"data": {
"tracking": "https://example.com?source=data_field"
},
"actions": [
{
"action": "yes",
"title": "Enter to the site",
},
{
"action": "no",
"title": "Ignore",
}
],
},
"fcm_options": {
"link": "https://example.com?source=fcm_options"
}
}
However, if I understand correctly from the Firebase documentation, I shouldn't need to do this. I can implement the URL in the payload as part of fcm_options.link, and this should handle the click operation without the need to handle it in the service worker.
But for some reason, it doesn't work for me - nothing happens when I click on the notification (if I remove the handling of the onclick from the service worker).
I would appreciate some assistance and clarification on how to handle the click behavior of web-push notifications by using the fcm_options.link field and without the need of the service worker.