Create a Firebase notification with a foreground / focus page

With Firebase Cloud Messaging (for the web), how can I generate a notification that appears when a webpage is closed or in the background, but when I'm actually focused on the webpage?

I understand that messaging.onMessage(...)this is where I process incoming messages when the page is in focus, but I cannot find documentation on how I can create notification pop-ups as if the page was in the background.

Thank you for your time!

+11
source share
2 answers

handle incoming messages using the notification API

messaging.onMessage(function(payload) {
    const notificationTitle = payload.notification.title;
    const notificationOptions = {
        body: payload.notification.body,
        icon: payload.notification.icon,        
    };

    if (!("Notification" in window)) {
        console.log("This browser does not support system notifications");
    }
    // Let check whether notification permissions have already been granted
    else if (Notification.permission === "granted") {
        // If it okay let create a notification
        var notification = new Notification(notificationTitle,notificationOptions);
        notification.onclick = function(event) {
            event.preventDefault(); // prevent the browser from focusing the Notification tab
            window.open(payload.notification.click_action , '_blank');
            notification.close();
        }
    }
});

Notification has expired.


   messaging.onMessage(function(payload) {
        local_registration.active.postMessage(payload);
     }

push sw.js

self.addEventListener('notificationclick', function(event) {
console.log('[firebase-messaging-sw.js] Received notificationclick event ', event);

var click_action = event.notification.data;
event.notification.close();
// This looks to see if the current is already open and
// focuses if it is
event.waitUntil(clients.matchAll({
    type: "window"
}).then(function(clientList) {
    for (var i = 0; i < clientList.length; i++) {
        var client = clientList[i];
        if (client.url == click_action  && 'focus' in client)
            return client.focus();
    }
    if (clients.openWindow)
        return clients.openWindow(click_action);
    }));

});
const showMessage = function(payload){
    console.log('showMessage', payload);
    const notificationTitle = payload.data.title;
    const notificationOptions = {
        body: payload.data.body,
        icon: payload.data.icon,
        image: payload.data.image,
        click_action: payload.data.click_action,
        data:payload.data.click_action
    };  


  return self.registration.showNotification(notificationTitle,notificationOptions); 
}   
messaging.setBackgroundMessageHandler(showMessage);

self.addEventListener('message', function (evt) {     
  console.log("self",self);
  showMessage( evt.data );
})
+24

:

messaging.onMessage(payload => {
  const {title, ...options} = payload.notification;
  navigator.serviceWorker.ready.then(registration => {
    registration.showNotification(title, options);
  });
});
0

Source: https://habr.com/ru/post/1659768/


All Articles