首页 > 解决方案 > Firebase FCM onLaunch 代码在单击通知时未运行功能

问题描述

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

我包括

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

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

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

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

标签: firebaseflutterdartgoogle-cloud-firestorefirebase-cloud-messaging

解决方案


好的,所以我遇到的问题是,当启动代码触发时,我的应用程序在加载启动页面时正在加载其他数据和函数等。我必须做的是在发射时我将通知保存在一个变量中。然后我在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;

},

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

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

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


推荐阅读