Flutter - myBackgroundMessageHandler never called in push notification

Viewed 391

I am using firebase_messaging library with firebase for push notification in flutter. The documentation says that myBackgroundMessageHandler is called when the app is in background. But it has not happened.

I would like to format the title and the body of the notification before it is shown. I am getting notifications when the app is in the background but that particular method is not called. Currently it just shows the exact text sent by backend in title and body. Is there something I have to do to enable(?) this method? I have it in my fcm.configure method:

_fcm.configure(
    onBackgroundMessage: myBackgroundMessageHandler,
)
static Future<Map<String, dynamic>> myBackgroundMessageHandler(Map<dynamic, dynamic> message) async {
  //some code here
  return message;
}

Thanks in advance!

1 Answers

I had the same problem. onBackgroundMessage apparently gets triggered only for data-messages.

See the difference here: https://firebase.google.com/docs/cloud-messaging/concept-options

With FCM, you can send two types of messages to clients:

  • Notification messages, sometimes thought of as "display messages." These are handled by the FCM SDK automatically.
  • Data messages, which are handled by the client app.

In my case I had to remove the notification (subject, body etc), but also styling like bigpicture completely. Try sending only a data object.

Related