gpt4 book ai didi

ios - CloudKit NSPersistentCloudKitContainer 推送通知不起作用

转载 作者:行者123 更新时间:2023-12-05 07:04:19 31 4
gpt4 key购买 nike

我正在努力解决以下问题。尽管进行了深入搜索,但我还没有成功。

我的开发环境是 macOS Big Sur Beta 下的 Xcode 12.0 beta(但是,问题在 Catalina 下的 Xcode 11.5 上出现相同)。我正在开发的应用程序是多平台的,macOS 10.16 和 iOS 13.5 目标。该应用使用 NSPersistentCloudKitContainer 为 CoreData 配置。

虽然 registerForRemoteNotifications() 在 iOS 目标中成功且没有任何问题,并且正确调用了 didRegisterForRemoteNotificationsWithDeviceToken,但在 macOS 目标中没有任何反应:didRegisterForRemoteNotificationsWithDeviceToken didFailToRegisterForRemoteNotificationsWithError 也不会被触发。显然,CloudKit 和 PushNotification 授权已在 macO 和 iOS 目标上正确配置。查询 isRegisteredForRemoteNotifications 在 macOS 上也返回 true ...

persistentContainer 定义代码在 macOS 和 iOS 上是相同的(注意完全默认,并且 automaticallyMergesChangesFromParent 已正确设置)

var persistentContainer: NSPersistentCloudKitContainer = {
let container = NSPersistentCloudKitContainer(name: "MyFinance")

guard let description = container.persistentStoreDescriptions.first else {
os_log("Failed to retrieve a persistent store description. Aborting application.", log: log, type: .error)
fatalError("###\(#function): Failed to retrieve a persistent store description.")
}
description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)

container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error {
guard let error = error as NSError? else { return }
os_log("Failed to load persistent store. Aborting application.", log: log, type: .error)
fatalError("###\(#function): Failed to load persistent stores:\(error)")
}
})
container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
container.viewContext.transactionAuthor = "xxx"

do {
try container.viewContext.setQueryGenerationFrom(.current)
} catch {
os_log("Failed to pin viewContext to the current generation. Aborting application.", log: log, type: .error)
fatalError("###\(#function): Failed to pin viewContext to the current generation:\(error)")
}
container.viewContext.automaticallyMergesChangesFromParent = true

return container
}()

最终结果是,虽然 CoreData 与推送通知的同步在 macOS -> iOS 的方向上得到了正确处理,但在相反的方向上没有任何反应(因为 macOS 上的推送通知没有被触发):从 iOS 到 macOS,CoreData 是仅在应用程序启动时重新对齐。

任何人都可以将自己定位在正确的方向吗?两天来我一直在用头撞墙,没有任何明智的结果......

附言运行 macOS 目标时还有另一个“症状”:任何时候在应用程序中触发 API 调用,尽管调用成功,但 Xcode 调试控制台中会出现以下消息:

nw_resolver_start_query_timer_block_invoke [Cxx] Query fired: did not receive all answers in time for xxx (the API url)

iOS 目标不会发生这种情况。我找不到合适的解释。我真的不知道这是否与推送通知问题有某种关系。搜索有关它的公共(public)领域没有乐趣。

最佳答案

在 macOS 上启用 SwiftUI 的多平台应用程序上,我没有收到来自通知的任何回调。

调用 app.registerForRemoteNotifications() 在 macOS big sur 上没有任何作用,我越来越晚了

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge,]) { [unowned self] (granted, error) in

}

未显示权限

在我看来这可能是一个错误。

关于ios - CloudKit NSPersistentCloudKitContainer 推送通知不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62956287/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com