Reputation: 21
It is possible to show notification only if app is on foreground? I do not need to show notifications in background.
My setBackgroundMessageHandler
in service worker looks like:
messaging.setBackgroundMessageHandler(function(payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
var notificationTitle = 'Background Message Title';
var notificationOptions = {
body: 'Background Message body.',
icon: '/firebase-logo.png'
};
});
I dont return showNotification
but is showing
Upvotes: 1
Views: 761
Reputation: 61
To protect others from wasting their time, as I did, it is impossible to handle push events only in the foreground. (a.k.a. silent pushes are not allowed)
Even if you change the message type from notification to data, the best you'll end up with will be a generic Chrome message saying "This site has been updated in the background.".
More on this here:
https://blog.pushpad.xyz/2022/09/this-site-has-been-updated-in-the-background/
Upvotes: 0
Reputation: 598603
Firebase Cloud Messaging has two types of messages:
Since you want to prevent the system from displaying the notification in the system tray, you should send a data message.
Upvotes: 1