gpt4 book ai didi

Flutter Firebase 消息传递 : open a new screen on background notification received

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

调用了后台处理程序,一切正常,但我不知道如何获取 BuildContext 实例以导航到新屏幕。我有这个处理程序


Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
var notificationData = message.data;
var view = notificationData['view'];

if(view == 'MessagesScreen') {
Map<String, dynamic> videoData = json.decode(
notificationData['video_data']);
VideoItem videoItem = VideoItem.fromJson(videoData);

Navigator.pushNamed(context, '/playerScreen', arguments:{videoItem});
} else {
view = '/$view';
Navigator.pushNamed(context, view);
}

return Future<void>.value();
}

这样称呼

await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);

我正在使用 Firebase 消息传递:9.1.2

知道如何实现吗?

谢谢

最佳答案

如果您想在应用终止时用户点击通知时进行导航,请尝试以下操作:

FirebaseMessaging.instance
.getInitialMessage()
.then((RemoteMessage? message) {
if (message == null) return;

var notificationData = message.data;
var view = notificationData['view'];

if(view == 'MessagesScreen') {
Map<String, dynamic> videoData = json.decode(
notificationData['video_data']);
VideoItem videoItem = VideoItem.fromJson(videoData);

Navigator.pushNamed(context, '/playerScreen', arguments:{videoItem});
} else {
view = '/$view';
Navigator.pushNamed(context, view);
}

});

检查这个例子:https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_messaging/firebase_messaging/example/lib/main.dart#L116

关于Flutter Firebase 消息传递 : open a new screen on background notification received,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67296077/

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