DEV Community

Dimitrios Desyllas
Dimitrios Desyllas

Posted on

Answer: How I can change a mobx state upon a receival of a background message from firebase?

At this question:

This is achievable by placing at firebase-messaging-sw.js the following:

messaging.onBackgroundMessage(function(payload) {
  console.log('Received background message', payload)
  // Propagate message upon UI
  if(typeof payload.data !== 'undefined' && typeof payload.data['flag'] !== 'undefined' ) {
    self.clients.matchAll({includeUncontrolled: true}).then(function (clients) {
      //you can see your main window client in this list.
      clients.forEach(function(client) {
          client.postMessage({...payload});
      });
    })

I showcase how to trigger a mobx state change upon receival of a background message at firebase FCM.

Hope it helps.

Top comments (0)