onBackgroundMessage not getting called flutter messaging

Viewed 308

Am getting a frustrating experience with firebase messaging on my flutter app as onBackgroundmessage is not getting triggered while app is in background after sending request. I would like to know why as this is a core feature my app needs.

  Future initialize(context) async{
    _firebaseMessaging.configure(
   
    onBackgroundMessage: Platform.isIOS ? null:_myBackgroundMessageHandler,

    
    );
  }
  Future<dynamic> _myBackgroundMessageHandler
      (Map<String, dynamic> message) async {
      print("onBackground Message called");
    fetchRideInfoInBackground();
    return Future<void>.value();
  }

}

Notification Payload

const message = {
      notification: {
        title: 'New Incoming Request',
        body: `New incoming ${service} request`,
      },
      data: { orderId: orderId },
      android: {
        ttl: 30,
        priority: 'high',
        notification: {
          click_action: 'FLUTTER_NOTIFICATION_CLICK',
        },
      },
      tokens: driversRegistrationToken,
    };
1 Answers

I don't see the whole file, but I assume your _myBackgroundMessageHandler is not a top level function.
Make it top level or static, after that it should work.

Edit: example


Future<dynamic> _myBackgroundMessageHandler (Map<String, dynamic> message) async {
    print("onBackground Message called");
    fetchRideInfoInBackground();
    return Future<void>.value();
}

 class PushHandler {
 
  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();

  Future initialize(context) async{
    _firebaseMessaging.configure(
      onBackgroundMessage: Platform.isIOS ? null:_myBackgroundMessageHandler,
    );
  }
 }
Related