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

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?

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.

Have you got solution.I stucked here.Please send me solution if yo got a solution,Thanks

You’ve to use service workers!

Service workers only works for PWA apps not on android or ios platform.I need for ios and android platform.

You’re sort of in a strange thread, then.

1 Like

That is indeed the topic of a different thread. There’s a number of little things that can go wrong with Firebase push notifications for Android and iOS. You just have to go through each flow, check that each step is working, and try to find a specific error message. The big one is to make sure you’re getting a token from the client. Once you know you have that, you can send a test message. If that fails you’ll have have more info to refine you’re trouble-shooting.