Home > Software engineering >  Firebase FCM push notification for web
Firebase FCM push notification for web

Time:02-01

Is it possible to open an external URL when the user click on FCM push notification on web. If it is possible can anyone give provide some details on it or any documentation. Thank you.

I tried firebase documentation and did some research online. There are ways to achieve it in android and ios devices, I think, but could not find anything for web. I am looking for someone with experience in this or anyone who can provide me with any documentation.

CodePudding user response:

Yes, it is possible to open an external URL when the user clicks on a FCM push notification on the web. You need to set a click_action field in the payload of your notification, with a value of "click_action":"https://www.example.com". Then you need to handle the notification click event in your web app to redirect the user to the specified URL.

Here is some sample code that shows how to handle the click event and redirect the user to the URL specified in the click_action field:

// Handle notification click
self.addEventListener('notificationclick', function(event) {
  event.notification.close();
  event.waitUntil(clients.openWindow(event.notification.data.click_action));
});

You can find more information on how to use FCM push notifications in a web app in the Firebase documentation: https://firebase.google.com/docs/cloud-messaging/js/receive

  • Related