【问题标题】:CloudKit NSPersistentCloudKitContainer push notifications not workingCloudKit NSPersistentCloudKitContainer 推送通知不起作用
【发布时间】:2020-11-07 09:31:12
【问题描述】:

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

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

虽然 registerForRemoteNotifications() 在 iOS 目标中成功成功,并且 didRegisterForRemoteNotificationsWithDeviceToken 被正确调用,但在 macOS 目标中没有任何反应:didRegisterForRemoteNotificationsWithDeviceTokendidFailToRegisterForRemoteNotificationsWithError 都没有被触发。显然,在 macOS 和 iOS 目标上都正确配置了 CloudKit 和 PushNotification 权利。在 macOS 上查询 isRegisteredForRemoteNotifications 也会返回 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 目标不会发生这种情况。我无法为此找到一个体面的解释。我真的不知道这是否与推送通知问题有关。在公共领域搜索它并不快乐。

【问题讨论】:

    标签: ios macos apple-push-notifications nspersistentcloudkitcontainer


    【解决方案1】:

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

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

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

    权限未显示

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-29
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 2014-08-06
      相关资源
      最近更新 更多