FCM - A background message could not be handled in Dart as no onBackgroundMessage handler has been registered

Viewed 1182

I did exactly as mentions in the docs, a Future method outside of main() and onBackgroundMessage inside it

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

main() async {
  FirebaseMessaging.onBackgroundMessage((message){
    return _firebaseMessagingBackgroundHandler(message);
  });
}

but when I run the app I get error:

Unhandled Exception: Null check operator used on a null value
E/flutter (10760): #0      MethodChannelFirebaseMessaging.registerBackgroundMessageHandler (package:firebase_messaging_platform_interface/src/method_channel/method_channel_messaging.dart:180:53)
E/flutter (10760): #1      FirebaseMessagingPlatform.onBackgroundMessage= (package:firebase_messaging_platform_interface/src/platform_interface/platform_interface_messaging.dart:102:16)
E/flutter (10760): #2      FirebaseMessaging.onBackgroundMessage (package:firebase_messaging/src/messaging.dart:73:31)
E/flutter (10760): #3      main (package:neox/main.dart:56:21)
E/flutter (10760): #4      main (file:///E:/Flutter_Projects/NeoX/.dart_tool/flutter_build/generated_main.dart:102:42)
E/flutter (10760): #5      _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:128:38)
E/flutter (10760): #6      _rootRun (dart:async/zone.dart:1426:13)
E/flutter (10760): #7      _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter (10760): #8      _runZoned (dart:async/zone.dart:1861:10)
E/flutter (10760): #9      runZonedGuarded (dart:async/zone.dart:1849:12)
E/flutter (10760): #10     _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:126:5)
E/flutter (10760): #11     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
E/flutter (10760): #12     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)

and when receiving a notification I get:

W/FLTFireMsgService(10760): A background message could not be handled in Dart as no onBackgroundMessage handler has been registered.
2 Answers

This is all the steps you need

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {

  // you need to initialize firebase first
  await Firebase.initializeApp();

  print("Handling a background message: ${message.messageId}");
}

main() async {
  WidgetsFlutterBinding.ensureInitialized();
   
  // initialize firebase
  await Firebase.initializeApp();

  // you can just pass the function like this
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

   await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
    alert: true,
    badge: true,
    sound: true,
  );
}

I think you are misreading a Documentation

Here is how it should be in your main.dart

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  // If you're going to use other Firebase services in the background, such as Firestore,
  // make sure you call `initializeApp` before using other Firebase services.
  await Firebase.initializeApp();

  print("Handling a background message: ${message.messageId}");
}

void main() {
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  runApp(MyApp());
}

And remember to config your AndroidManifest.xml

<service
    android:name=".java.MyFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

When you face this Error: java.lang.RuntimeException: Unable to create service rw.aos.im.java.MyFirebaseMessagingService: java.lang.ClassNotFoundException: Didn't find class "com.example.yourapp.java.MyFirebaseMessagingService"

Please use the following

<service
    android:name="com.google.firebase.messaging.FirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

Make sure that all of the following setups is configured correctly

  1. https://firebase.google.com/docs/cloud-messaging/android/client
  2. https://firebase.google.com/docs/android/setup

Hope this will solve your issue

Related