【问题标题】:Flutter firebase receiving notification when closed or in background - how to pass message to classFlutter firebase在关闭或后台接收通知-如何将消息传递给类
【发布时间】:2022-06-20 00:23:22
【问题描述】:

我正在使用 Firebase 推送通知构建 Flutter 应用。

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

  • 当应用程序处于前台时,弹出模式会显示 - 这有效

  • 当应用程序是后台并且移动设备接收到消息时,它会显示在系统托盘中,用户单击它,应用程序打开,并找到初始消息并在弹出模式中显示给用户 - 例如. FirebaseMessaging.onMessageOpenedApp 函数 - 这行得通。

  • 当应用程序在后台时,手机收到通知(并且firebase监听器正在工作,因为它使用debugPrint输出消息数据进行测试),它出现在系统托盘中,但用户选择不单击消息 - 当应用程序回到前台时,消息被忽略 - 这是一个问题。

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

请在下面查看我的代码(请参阅最后几行以了解我遇到的问题)...

  //TOP LEVEL-----
  Future<void> _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<void> main() async {
       await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
       FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
       runApp(MyApp());
  }

  //APP CLASS-----
  class MyAppextends StatefulWidget {        
       State<MyApp> createState() => _MyAppState();
  }

       
  //APP STATE CLASS
  class _MyAppState extends State<MyApp>  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)
       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<void> _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??
                     **/

             }

        }

【问题讨论】:

    标签: firebase flutter push-notification firebase-cloud-messaging


    【解决方案1】:

    将消息保存到持久存储,并在应用程序启动时检查存储是否有待处理的消息。

    【讨论】:

    • 谢谢 - 我已经尝试过了,虽然没有错误,但当我从存储中调用信息时,它是空白的(即使我打印出要检查的值)它保存了数据)。
    • 我还尝试在初始化阶段创建一个对象并将其推送到类,当在后台收到消息时,它会修改对象以存储消息的详细信息 - 但又一次,从班级内检查时,它始终为空白。我只能假设在初始化阶段将对象传递给类时,它传递的是该对象的副本而不是指针引用,因此即使对象数据发生更改,发送给类的对象副本仍然存在不变。
    • 请分享代码,将消息保存到后台处理程序的持久存储中,然后在应用程序启动时读取它。
    【解决方案2】:

    我也遇到了这个问题,这很烦人,但感谢thread,我找到了解决方案。 FCM 的文件指出:

    收到后,将生成一个隔离(仅限 Android,iOS/macOS 不 需要单独的隔离)允许您处理消息,即使在 您的应用程序没有运行。

    重要的一点是,每个隔离区都有自己的内存,因此您在前台的共享偏好与在后台的偏好不同。您可以在应用恢复时调用 SharedPreference.reload() 来“同步”数据。

    【讨论】:

      猜你喜欢
      • 2019-10-08
      • 1970-01-01
      • 2021-02-19
      • 2017-01-13
      • 2017-02-06
      • 2019-07-05
      • 2021-07-22
      • 2020-11-23
      • 2020-07-28
      相关资源
      最近更新 更多