Search code examples
firebaseweb-pushfirebase-admin

Firebase messaging `setBackgroundMessageHandler` overrides notification body #AskFirebase


I'm using the new admin.messaging().sendToDevice() method to send Web Push notifications. I'm sending both notification and data so I can show users some context in the message.

On client-side I'm using firebase.messaging().setBackgroundMessageHandler() to handle receiving messages.

The problem is that the setBackgroundMessageHandler() never gets called. I tried the solution from a similar question but if I alter the payload structure on the server to add the notification in the data field an error is thrown saying I should only use strings as values in the data (the notification is an object. see admin.messaging.NotificationMessagePayload).

If I don't use firebase.messaging on the server and just restructure as the other answer suggested, I won't be able to enjoy the wonderful feature of clickAction which saves me handling the notificationclick myself.

I also added listener to the push event on the service worker and I receive the full payload (both notification and data are available), it just doesn't call the callback

Is it a bug with firebase.messaging.setBackgroundMessageHandler()? Or am I not using it how I'm supposed to?


Solution

  • I contacted Firebase support and they said that's the expected behavior.

    So if one wants to do something when a push happens he should only send the data field. If a notification field is sent, it will take precedence and the callback won't be called but the user will get a notification.