setOnBackgroundCallback method

void setOnBackgroundCallback(
  1. NotificationCallback? callback
)

Sets the callback for background notifications.

This callback will be called when a notification is received while the app is in the background or terminated. This method allows changing the callback after initialization.

Parameters:

  • callback: Function that receives a RemoteMessage containing the notification data

Example:

SuperFCM.instance.setOnBackgroundCallback(backgroundHandler);

// ...

// When using Flutter version 3.3.0 or higher, the message handler must be
// annotated with @pragma('vm:entry-point') right above the function declaration
// (otherwise it may be removed during tree shaking for release mode)
@pragma('vm:entry-point')
Future<void> backgroundHandler(RemoteMessage message) async {
  print('New notification received: ${message.notification?.title}');
}

Implementation

void setOnBackgroundCallback(NotificationCallback? callback) {
  logger.d('Setting new onBackground callback');
  FirebaseMessagingService.instance
      .onBackgroundMessage(callback ?? _superFCMBackgroundHandler);
}