standy
standy

Reputation: 446

Firebase messaging "the site has been updated in background"

I've got a problem. When getting a notification in my service worker:

messaging.setBackgroundMessageHandler(function(payload) {
  console.log('[firebase-messaging-sw.js] Received background message ', payload);
  angularClient.postMessage(payload.data);
});

How can I prevent the browser from showing notification that says:

the site has been updated in background

What I do is sending a message to my service where the notification will be showed. For now it shows two notifications. One from my service which is fine and another saying "the site... ".

Upvotes: 5

Views: 8668

Answers (3)

Acid Coder
Acid Coder

Reputation: 2746

firebase v10 answer, you have to return the show notification promise

import { getMessaging, onBackgroundMessage } from 'firebase/messaging/sw'
onBackgroundMessage(getMessaging(app), payload => {
    const { title, body } = payload.data

    return (
        self.registration
            .showNotification(title, { body })
    )
})

Upvotes: 0

ulu
ulu

Reputation: 6092

I was getting this notification in addition to the one I created myself. Spent two days trying to fix it. This is the code that worked for me:

self.addEventListener('push', async function(event) {
    event.waitUntil(
        self.registration.showNotification('title', {
          body: 'body'
        })
    );
});

Upvotes: 2

Matt Gaunt
Matt Gaunt

Reputation: 9821

You MUST show a notification when onBackgroundMessage is called and make sure your return the promise from registration.showNotification('title', {body: 'message'})

The reason for this is that web push doesn't support silent messages.

Upvotes: 8

Related Questions