FR: Please help add an option to enable / disable auto notifications on Firebase Messaging.
Operating System
MacOS 14.6.1
Environment (if applicable)
Chrome 133.0.6943.142
Firebase SDK Version
^11.3.1
Firebase SDK Product(s)
Messaging
Project Tooling
I built a Google Extension with Firebase Messaging
Detailed Problem Description
I am facing an issue with duplicate notifications between my custom notification and Firebase auto-notification. I am using a single Firebase project for both Android and the Extension, causing duplication.
Steps and code to reproduce issue
Init Firebase Messaging and add more
self.registration.showNotification('This is title', {
title: '',
body: 'This is body',
icon: 'images/logo.png',
data: {
...
},
})
Hi @tiennguyen1293,
By Auto Notifications, do you mean notifications originating form Firebase Services like Database, etc. I believe this would require a Firebase service change and we can't achieve this by updating the SDK's API. I recommend reaching out to Firebase Support, they should be able to handle this feature request.
If I'm misunderstanding then please let us know. Thanks!
Thank you for your quick response, @DellaBitta.
I am using onBackgroundMessage from firebase/messaging/sw, and this triggers an automatic push notification if my data message contains notification:
{
"to": "<FCM_TOKEN>",
"data": {
"title": "Your Custom Title",
"body": "Your Custom Message",
"customKey": "customValue"
},
"content_available": true,
"priority": "high",
"notification": {
"title": "Hello User!",
"body": "This is an auto-displayed notification",
"image": "https://example.com/image.jpg"
}
}
However, I cannot remove notification since it is related to other platforms using the same Firebase project.
That’s what I mean—do we have a way to prevent this from initializeApp?
import { initializeApp, getApps } from 'firebase/app'
import { getToken } from 'firebase/messaging'
app = initializeApp({ ...firebaseConfig, isDisableAutoPushNotification: true })
const messaging = getMessaging(app)
Thanks for your help.
Ok, I see. Confirmed, onBackgroundMessage() does always display a notification when received, if the payload has a "notification" property. We'll count this is a feature request.
Operating System
MacOS 14.6.1
Environment (if applicable)
Chrome 133.0.6943.142
Firebase SDK Version
^11.3.1
Firebase SDK Product(s)
Messaging
Project Tooling
I built a Google Extension with Firebase Messaging
Detailed Problem Description
I am facing an issue with duplicate notifications between my custom notification and Firebase auto-notification. I am using a single Firebase project for both Android and the Extension, causing duplication.
Steps and code to reproduce issue
Init Firebase Messaging and add more
self.registration.showNotification('This is title', { title: '', body: 'This is body', icon: 'images/logo.png', data: { ... }, })
Hi @hsubox76 & @DellaBitta, I’d like to contribute the suppressAutoNotification flag requested here. Plan: add an optional boolean to onBackgroundMessage so the service-worker can skip the automatic showNotification() call while still letting developers call it manually. Any objections or preferred API shape before I open a PR? Thanks!
Hi @singhaditya73
We will need the approval of the Firebase Messaging team API changes, so I'll need to bring a proposal to their attention.
Instead of going straight to a PR, could you post the proposed API change here so that someone from the Messaging SDK team can discuss it with you?
Thanks!
Hi @DellaBitta,
Final proposal after walking the actual code in sw-listeners.ts lines 97-116.
Root issue
showNotification() fires before the developer's onBackgroundMessage handler, so suppression is impossible.
Minimal fix
Reorder + inject a one-time suppressor with fail-safe error handling:
let autoNotificationSuppressed = false;
const suppressAutoNotification = () => { autoNotificationSuppressed = true; };
if (!!messaging.onBackgroundMessageHandler) {
const payload = externalizePayload(internalPayload);
try {
if (typeof messaging.onBackgroundMessageHandler === 'function') {
await messaging.onBackgroundMessageHandler(payload, { suppressAutoNotification });
} else {
messaging.onBackgroundMessageHandler.next(payload);
}
} catch (err) {
// Handler error – still show notification (fail-safe)
console.error('onBackgroundMessage handler error:', err);
}
}
// show notification only if not suppressed
if (!!internalPayload.notification && !autoNotificationSuppressed) {
await showNotification(wrapInternalPayload(internalPayload));
}
Type signature (backward-compatible)
type BackgroundMessageHandler =
| ((payload: MessagePayload, ctx?: { suppressAutoNotification: () => void }) => void | Promise<void>)
| Observable<MessagePayload>;
No breaking changes; Observable path untouched. Happy to open a PR once approved.
I've notified someone from the Messaging SDK team asking them to review your proposal. Thanks so much!