【问题标题】:Firebase FCM onLaunch code not running functions when clicking on notificationFirebase FCM onLaunch 代码在单击通知时未运行功能
【发布时间】:2020-10-11 11:38:10
【问题描述】:

我正在使用 FCM 传递通知,一旦单击,就会将用户带到应用程序上的特定页面。目前,onMessage 和 onResume 函数可以正常工作,但在启动时却不行。

我包括

<key>FirebaseAppDelegateProxyEnabled</key>
<false/>

和

if #available(iOS 10.0, *) {
  UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}

因为我也在使用颤振本地通知。

我已经尝试删除这些行,但它们没有任何区别。

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore firebase-cloud-messaging


    【解决方案1】:

    好的,所以我遇到的问题是,当启动代码触发时,我的应用程序在加载启动页面时正在加载其他数据和函数等。我必须做的是在发射时我将通知保存在一个变量中。然后我在splash init完成后执行了我想要的代码

    onLaunch: (Map<String, dynamic> notification) async {
        print("onLaunch: $notification");
    
        ///Saving the notification to use once the rest of the initialising and 
        ///Loading is done
        launchNotification = notification;
    
    },
    

    然后,一旦其他进程的加载和初始化完成,我就运行了 run this 函数

    onLaunchFunction() async {
      await Future.delayed(Duration(milliseconds: 100));
      Map tempNotification = launchNotification;
      if (launchNotification != null) {
        launchNotification = null;
        ///The rest of the function
      }
    }
    

    我添加了一个延迟的未来,以确保我的代码在初始化时运行。这可能不需要

    【讨论】:

    • 你还在用这种方式吗?你有没有开发出另一种(更清洁——如果我可以这么说的话)方式?
    • @MohammedAl-sadi 我没有更改它,因为它可以正常工作。可能有更好的方法我没有研究过。如果你找到了,请告诉我?谢谢
    • 感谢@GILO 的回复。我今天才发现(阅读您的答案后)使用 SchedulerBinding 的能力。它在完成 UI 渲染后回调任何函数(阅读更多关于它的信息,我仍然不擅长颤动)。这是一个示例代码:SchedulerBinding.instance.addPostFrameCallback( (_) => YourFunctionComesHere());
    猜你喜欢
    • 2021-01-06
    • 1970-01-01
    • 2020-02-02
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 2016-09-30
    • 2017-05-04
    • 2019-10-15
    相关资源
    最近更新 更多