Push notification is working fine, but setBackgroundMessageHandler in firebase-messanging-sw.js is not getting called

Viewed 56

I am using firebase push notification in my angular application, and I want to add an icon in the notification, and when the user clicks that notification redirect to a specific route in the application, in order to achieve that I put event setBackgroundMessageHandler and self.eventListner('notificationClick') but that didn't get called.

  • Angular version (13.1.1)
  • @angular/fire version (^6.0.4)
  • firebase version (^8.6.5)

firebase-messanging-sw.js file code

importScripts('https://www.gstatic.com/firebasejs/8.6.5/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/8.6.5/firebase-messaging.js');


firebase.initializeApp({
    apiKey: "",
    authDomain: "",
    databaseURL: "",
    projectId: "",
    storageBucket: "",
    messagingSenderId: "",
    appId: "",
    measurementId: ""
});

const messaging = firebase.messaging();

messaging.setBackgroundMessageHandler(function (payload) {
  const notificationOptions = {
    title: 'My-App',
    body: payload.data.body,
    icon: '/images/logo.png',
    data: payload.data,
    color: '#ff0000'
  };

  return self.registration.showNotification('My Title',
      notificationOptions);
});

self.addEventListener('notificationclick', function (event) {
  event.notification.close();
  
  event.waitUntil(
    clients.openWindow("https://stackoverflow.com/questions")
  );
});

Notification is working fine but the problem is that method is not get called, I am testing all changes in localhost:4200/

0 Answers
Related