我完全按照文档中提到的那样做了,main()
之外的Future方法和内部的onBackgroundMessage
方法
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async{
print("Handling a background message: ${message.messageId}");
}
main() async {
FirebaseMessaging.onBackgroundMessage((message){
return _firebaseMessagingBackgroundHandler(message);
});
}
但当我运行应用程序时,我会出现错误:
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)
当我收到通知时:
W/FLTFireMsgService(10760): A background message could not be handled in Dart as no onBackgroundMessage handler has been registered.
这是您需要的所有步骤
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,
);
}
我认为您误读了文档
以下是它在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());
}
请记住配置AndroidManifest.xml
<service
android:name=".java.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
当您遇到此错误时:java.lang.RuntimeException:无法创建服务rw.aos.im.java.MyFirebaseMessagingService:java.lang.ClassNotFoundException:找不到类"com.example.yourapp.java.MyFirebaseMessagingService">
请使用以下
<service
android:name="com.google.firebase.messaging.FirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
确保以下所有设置均正确配置
- https://firebase.google.com/docs/cloud-messaging/android/client
- https://firebase.google.com/docs/android/setup
希望这将解决您的问题