firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

FR: Please help add an option to enable / disable auto notifications on Firebase Messaging.

Open tiennguyen1293 opened this issue 9 months ago • 5 comments

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: {
         ...
      },
    })

tiennguyen1293 avatar Feb 28 '25 09:02 tiennguyen1293

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!

DellaBitta avatar Feb 28 '25 20:02 DellaBitta

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.

tiennguyen1293 avatar Mar 03 '25 01:03 tiennguyen1293

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.

hsubox76 avatar Mar 06 '25 20:03 hsubox76

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: {
         ...
      },
    })

HourtashHAJI avatar Mar 06 '25 22:03 HourtashHAJI

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!

singhaditya73 avatar Dec 08 '25 16:12 singhaditya73

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!

DellaBitta avatar Dec 10 '25 18:12 DellaBitta

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.

singhaditya73 avatar Dec 11 '25 15:12 singhaditya73

I've notified someone from the Messaging SDK team asking them to review your proposal. Thanks so much!

DellaBitta avatar Dec 12 '25 13:12 DellaBitta