Can't get notification payload from firebase-sw on Ionic PWA

#1

I’ve searched a lot for this problem that I’m stuck on for a week. I have an Ionic PWA project that receive some notifications from firebase, I can receive the notifications with no problem and can get the payload on foreground (app is open), but I cant get the payload when the app is closed and I don’t figure out what is goning on, so I came here to ask the masters to help me.

messaging.setBackgroundMessageHandler(function(payload) {
    var notificationTitle = 'Teste';
    var notificationOptions = {
      body: 'Background Message body.',
      tag: 'campanhas',
      data: payload.data,
      icon: 'https://firebasestorage.googleapis.com/v0/b/hemotomobile-edeb9.appspot.com/o/logo.png?alt=media&token=4a9fc487-b8cf-4d3c-875c-774454ff6f50'
   };


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

self.addEventListener('notificationclick', function(event) {
   event.notification.close();

   event.waitUntil(clients.matchAll({
     type: "window"
   }).then(function(clientList) {
     for (var i = 0; i < clientList.length; i++) {
       var client = clientList[i];
       if (client.url == '/' && 'focus' in client)
       return client.focus();
     }
     if (clients.openWindow)
      return clients.openWindow('/');
   }));
});

and on my provider for the notifications, I use this code:

public receiveMessage() {
  console.log('notification')
  this.messaging.onMessage((payload) => {
    console.log('payload',payload)
  });
}

I call this provider on my tabs-page:

ionViewDidLoad(){
  this.notification.receiveMessage()
}

So, can anyone help me to get the payload when the PWA is closed?

#2

I’m having the same problem! I tried @ionic-native/fcm and then @ionic-native/firebase-messaging. Both seem to promise this functionality but I haven’t been able to get either to work.

I get the push notification just fine but the callback here never fires.

this.firebaseMessaging.onBackgroundMessage().subscribe(message => {
               console.log(‘got message in background’,message);
               this.savedChats.push(message);
           })

I’d expect that this would save the data from the notification but no. I can find examples for the cordova onBackgroundMessage but not the ionic wrapper. I can’t imagine there’s a function for onBackgroundMessage but no ability to receive and process background messages.