我正在使用“AppLifecycleState”推送通知构建Flutter应用(图)

我正在使用 Firebase 推送通知构建一个 Flutter 应用程序。

我希望应用在收到消息时显示带有文本的弹出模式。

“FirebaseMessaging.onBackgroundMessage”函数需要放置在 TOP LEVEL(任何类之外)。那么,当应用程序再次被带到前台时,如何将应用程序在后台时可能收到的消息中的消息数据推送到我的 App Class 以显示消息内容?我正在使用“AppLifecycleState”来检测应用程序何时返回前台,但我无法获取消息数据,因为它是在顶层而不是在类中接收的。

请参阅下面的代码(有关我遇到的问题,请参阅最后几行)…

  //TOP LEVEL-----
  Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
    if (message.messageId!="") {
        debugPrint("Have received a background message! Will have to grab the message from here somehow if the user didn't interact with the system tray message link");
    } 
  }
  Future main() async {
       await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
       FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

图片[1]-我正在使用“AppLifecycleState”推送通知构建Flutter应用(图)-唐朝资源网

runApp(MyApp()); } //APP CLASS----- class MyAppextends StatefulWidget { State createState() => _MyAppState(); } //APP STATE CLASS class _MyAppState extends State with WidgetsBindingObserver{ @override void initState() { super.initState(); _initiateNotificationForegroundListener(); _initiateInteractedMessage(); } // This variable will tell you whether the application is in foreground or not. bool _isInForeground = true; //Initiate Foreground Notification Listener (works)

图片[2]-我正在使用“AppLifecycleState”推送通知构建Flutter应用(图)-唐朝资源网

void _initiateNotificationForegroundListener() { FirebaseMessaging.onMessage.listen((RemoteMessage message) { _handleNotificationInstruction(message); }); } //Initiate Background/Closed Notification Listener if user clicks the message in the system try (works) Future _initiateInteractedMessage() async { RemoteMessage? message = await FirebaseMessaging.instance.getInitialMessage(); if (message != null) { _handleNotificationInstruction(message); } // When app is in background (Stream listener) FirebaseMessaging.onMessageOpenedApp .listen(_handleNotificationInstruction); } void _handleNotificationInstruction(RemoteMessage message) { //Create popup to display message info (works) } //Detect when an app moves in to the foreground @override void didChangeAppLifecycleState(AppLifecycleState state) { super.didChangeAppLifecycleState(state); _isInForeground = state == AppLifecycleState.resumed; if(_isInForeground){ /** HELP!!! /* How can I check what message might have been received while app was in the background?? ie. the top-level _firebaseMessagingBackgroundHandler function?? **/ } }

© 版权声明
THE END
喜欢就支持一下吧
点赞136赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容