gpt4 book ai didi

firebase - Flutter FCM onBackgroundMessage 不适用于嵌套的非静态方法调用

转载 作者:行者123 更新时间:2023-12-03 03:04:36 25 4
gpt4 key购买 nike

我正在使用 firebase_messaging 6.0.9flutter 1.12.13 .在 repo 的自述文件中:https://pub.dev/packages/firebase_messaging#-readme-tab-它说要声明 onBackgroudMessage回调作为静态或顶级方法。我做了,但是当这个回调调用非静态方法时它不起作用。以下示例使用单例类演示了这一点:

class NotificationService {

static NotificationService _instance;

final FirebaseMessaging _firebase;

static NotificationService get instance => _instance;

NotificationService._internal() : this._firebase = FirebaseMessaging();

factory NotificationService() {
if (_instance == null) {
_instance = NotificationService._internal();
_instance._firebase.configure(
onBackgroundMessage: NotificationService.staticHandler
);
}

return _instance;
}

static Future<dynamic> staticHandler(Map<String, dynamic> msg) {
print("Static Func >>> $msg"); // Successfully prints
return NotificationService.instance.instanceFunc(msg); // Fails here, complaining that it's being invoked on null.
}

Future<dynamic> instanceFunc(Map<String, dynamic> msg) {
print("Instance Func >>> $msg");
}

void myVarFunc() {
print("This is my var func");
}
}

main.dart ,调用通知服务工厂构造函数:
import 'package:myProject/services/notification/notification_service.dart';

run(MyApp());

class MyApp extends StatelessWidget {
final NotificationService _ns = NotificationService();
NotificationService.instance.myVarFunc(); // Prints successfully.
.......
.......
.......
}

instanceFunc 的调用失败,说它是在 null 上调用的。以下是日志:
I/flutter ( 6935): Static Func >>> {data: {title: Title_is_here, message: Message_is_here}}
I/flutter ( 6935): Unable to handle incoming background message.
I/flutter ( 6935): NoSuchMethodError: The method 'instanceFunc' was called on null.
I/flutter ( 6935): Receiver: null
I/flutter ( 6935): Tried calling: instanceFunc(_LinkedHashMap len:1)

我不确定这是否是处理这种情况的正确方法。由于我是 Dart 和 Flutter 的新手,我的知识非常有限。我不能声明一切都是静态的并且可以工作,这不是 IMO 的好设计。我可能在这里遗漏了一些东西。

最佳答案

有一些原因没有在 onBackgroundMessage 上获得回调:-

  • onBackgroundMessage 在 iOS 上不起作用,所以你必须实现平台检查

  • 例如:-
        _firebaseMessaging.configure(
    onMessage: (Map<String, dynamic> message) async {
    printDebug("onMessage foreground: $message");
    },
    onBackgroundMessage: Platform.isIOS ? null : _myBackgroundMessageHandler,
    onLaunch: (Map<String, dynamic> message) async {
    printDebug("onLaunch Kill: $message");
    },
    onResume: (Map<String, dynamic> message) async {
    print("onResume Background: $message");
    },
    );
    static Future<dynamic> _myBackgroundMessageHandler(
    Map<String, dynamic> message) async {
    print("onBackgroundMessage: $message");
    return Future<void>.value();
    }
  • 并且您必须确保您的通知有效负载不包含通知 key ,因为如果有效负载中存在通知 key ,那么通知将由您的系统直接处理。因此,您必须从有效负载中删除通知键才能在 onBackgroundMessage 上获得回调。

  • 注意:- 如果您删除通知键,则通知不会在系统通知托盘中呈现。为此,您可以在本地通知。

    关于firebase - Flutter FCM onBackgroundMessage 不适用于嵌套的非静态方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60308220/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com