firebase / quickstart-js

Firebase Quickstart Samples for Web
https://firebase.google.com
Apache License 2.0
5.09k stars 3.66k forks source link

How can I prevent to show notifications on background? #559

Open damandeepsinghbhatia opened 3 years ago

damandeepsinghbhatia commented 3 years ago

I have tried messaging.setBackgroundMessageHandler(function(payload){

}); unfortunately this is not working. I am using 8.2.9 version importScripts('https://www.gstatic.com/firebasejs/8.2.9/firebase-app.js'); importScripts('https://www.gstatic.com/firebasejs/8.2.9/firebase-messaging.js');

csurf commented 3 years ago

I have the same question. It appears that the setBackgroundMessageHandler event handler has been deprecated & no longer works.

The onBackgroundMessage handler only allows you to customize a notification, but it doesn't provide a means to completely prevent all notifications from showing; FCM will still show a default notification, even if this event handler is defined as a dummy function.

How does one go about implementing logic to conditionally display a background notification???

EDIT: nevermind, I found a work-around, which is to setup my push notification as a data-only message (i.e., don't include a 'notification' field in the push request), and handle the creation of the notification object within my 'onBackgroundMessage' handler...

Chris-LuYi commented 3 years ago

@csurf brilliant!!!

UncleWeb1992 commented 1 year ago

yes but ios not worked if field notification exclude

Chooyinkeat commented 11 months ago

can you teach me how to do that? I dun get how to remove notification field works