如何解决Swift Firebase RemoteConfig:如何在后台 fetchAndActivate
我的 iOS 应用有一项默认禁用的新功能。为了管理这一点,我使用 Firebase RemoteConfig 来执行此操作,并将 new_feature_enabled = false
添加到 config_debug.plist
文件中。然后我使用 remoteConfig.setDefaults(fromPlist: "config_debug")
将其设置为默认值。
我的应用有一个后台任务,当收到无提示推送通知时,它会在后台唤醒 iOS 应用以执行一些任务。
我面临的问题是,在 Firebase 控制台上远程启用新功能(即在控制台上设置 new_feature_enabled = true
)并触发静默推送通知后,应用程序唤醒但该功能恢复为禁用模式。然后我终止了该应用程序,并再次启动了该应用程序,然后启用了该功能。
换句话说,每次设备收到静默推送通知并在后台唤醒时,remoteConfig 都会重置为本地 config.plist
文件,而不是使用 Firebase 控制台上设置的远程版本。
代码:
//In AppDelegate
func application(_ application: UIApplication,didReceiveRemoteNotification userInfo: [AnyHashable : Any],fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
//handle silent PN and do background tasks...
handleSilentPN(userInfo: userInfo)
completionHandler(.newData)
}
func handleSilentPN(userInfo: [AnyHashable: Any]) {
guard let scheduler = userInfo["scheduler"] as? [String: AnyObject] else {return}
guard let rootViewController = (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window?.rootViewController as? MainTabController else {
return
}
let vc = SomeController()
let controller = UINavigationController(rootViewController: vc)
rootViewController.present(controller,animated: false)
}
//At SceneDelegate
func scene(_ scene: UIScene,willConnectTo session: UISceneSession,options connectionOptions: UIScene.ConnectionOptions) {
//Setup remote config
setupRemoteConfig()
let vc = MainTabController()
setupWindow(viewController: vc,scene: scene)
}
fileprivate func setupWindow(viewController: UIViewController,scene: UIScene) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.windowScene = windowScene
window?.rootViewController = viewController
}
func setupRemoteConfig() {
let remoteConfig = RemoteConfig.remoteConfig()
//Throttles for development to see changes frequently
#if DEBUG
let settings = RemoteConfigSettings()
settings.minimumFetchInterval = 0
remoteConfig.configSettings = settings
#endif
//Define a default setting
#if DEBUG
remoteConfig.setDefaults(fromPlist: "config_debug")
#else
remoteConfig.setDefaults(fromPlist: "config_release")
#endif
remoteConfig.fetchAndActivate { (status,error) in
if let error = error {
Log("Err setingup remote config: \(error.localizedDescription)")
}
//0: .successFetchedFromRemote
//1: .successUsingPreFetchedData
//2: .error
Log("Remote config status: \(status.rawValue)")
}
}
我的代码在 SceneDelegate 同步 RemoteConfig,并确保它们是从 Firebase 控制台获取的远程版本。我怀疑 setupRemoteConfig
在 didReceiveRemoteNotification
之前被调用,因此没有同步远程版本。
我再次尝试将 setupRemoteConfig
移至 didFinishLaunchingWithOptions
和 didReceiveRemoteNotification
,但两者似乎都给出了相同的结果。
然后我测试了当应用在后台唤醒时哪些函数会被调用,并注意到以下不会被调用:
didFinishLaunchingWithOptions
application(_ application: UIApplication,configurationForConnecting connectingSceneSession: UISceneSession,options: UIScene.ConnectionOptions)
scene(_ scene: UIScene,options connectionOptions: UIScene.ConnectionOptions)
sceneDidBecomeActive(_ scene: UIScene)
我应该在哪里调用setupRemoteConfig()
,以便在收到静默 PN 后维护 remoteConfig 的远程版本,并确保启用新功能?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。