setBackgroundMessageHandler static method

void setBackgroundMessageHandler(
  1. Future<void> handler(
    1. RemoteMessage message
    )
)

Sets the handler for notifications received in the background.

This static method must be called in main.dart before runApp() to handle notifications that arrive when the app is in the background or terminated. The handler runs in a separate isolate.

Parameters:

  • handler: Callback function that processes background notifications

Example:

void main() {
  PushNotificationBloc.setBackgroundMessageHandler(
    _firebaseMessagingBackgroundHandler
  );
  runApp(MyApp());
}

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  print('Handling background message: ${message.messageId}');
}

Implementation

static void setBackgroundMessageHandler(
    Future<void> Function(RemoteMessage message) handler) {
  FirebaseMessaging.onBackgroundMessage(handler);
}