【问题标题】:Flutter FCM onBackgroundMessage handler gives null check errorFlutter FCM onBackgroundMessage 处理程序给出空检查错误
【发布时间】:2021-08-03 17:53:15
【问题描述】:

我正在尝试为我的应用实现通知,但是在初始化通知时 FirebaseMessaging.onBackgroundMessage(notificationHandler)gives 错误

错误:

E/flutter (28265): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] 未处理异常:空值检查运算符用于空值 E/flutter (28265): #0 MethodChannelFirebaseMessaging.registerBackgroundMessageHandler (package:firebase_messaging_platform_interface/src/method_channel/method_channel_messaging.dart:173:53) E/flutter (28265): #1 FirebaseMessagingPlatform.onBackgroundMessage= (package:firebase_messaging_platform_interface/src/platform_interface/platform_interface_messaging.dart:108:16) E/flutter (28265): #2 FirebaseMessaging.onBackgroundMessage (package:firebase_messaging/src/messaging.dart:100:31)

通知处理程序:

Future<void> notificationHandler(RemoteMessage message) async {
    /// do sth with data
}

注意:FirebaseMessaging.onMessageFirebaseMessaging.onMessageOpenedApp 可以正常工作

【问题讨论】:

  • 在主函数上初始化ònBackgroundMessage`解决了这个问题

标签: firebase flutter notifications firebase-cloud-messaging


【解决方案1】:

您似乎将FirebaseMessaging.onBackgroundMessage(notificationHandler)notificationHandler 函数放在一个类中。然而,这与 FlutterFire here 的官方文档相矛盾。您需要满足以下条件才能使此功能正常工作:

  1. 不能是匿名函数。
  2. 它必须是顶级函数(例如,不是需要初始化的类方法)。

我对您的建议是,尝试将您正在使用的函数 notificationHandler 作为参数您正在调用 FirebaseMessaging.onBackgroundMessage() 的类之外。它对我有用。

【讨论】:

    猜你喜欢
    • 2021-04-28
    • 2014-08-14
    • 2021-10-19
    • 1970-01-01
    • 2021-04-05
    • 2022-11-30
    • 2021-09-15
    • 2021-04-22
    • 1970-01-01
    相关资源
    最近更新 更多