Search code examples
firebase-cloud-messagingservice-workerweb-push

Firebase 8.2.0 onBackgroundMessage Shows undefined Duplicate Message


What's not working: I'm getting two push notifications. One with undefined body and title.

This happens after I upgrade to firebase 8.2.0 and use firebase.messaging().onBackgroundMessage function in my service worker:

import firebase from "firebase/app";
import "firebase/messaging";
import { CONFIG } from "./constants";

const config = process.env.NODE_ENV === "production" ? CONFIG.PROD : CONFIG.QA;
firebase.initializeApp(config);

const messaging = firebase.messaging();

messaging.onBackgroundMessage(function(payload) {
  console.log('[firebase-messaging-sw.js] Received background message ', payload);
  // Customize notification here
  const notificationTitle = payload.title;
  const notificationOptions = payload;

  self.registration.showNotification(notificationTitle,
    notificationOptions);
});


Solution

  • If it's not a data-only notification, the notification is shown by default. Your call of showNotification shows it once more. Either remove your onBackgroundMessage handler, or use data-only notifications. If the default notification contains less info than you need, you probably send not enough data in the notification. Add more info if you need to help with this.