2

我通过将 userInfo 转换为 CKDatabaseNotification 来处理私有和共享数据库通知。但是我确实在 didReceiveRemoteNotification 方法中也收到了公共数据库通知,并且 Apple 模板代码没有显示如何处理它并且它引发了一个 fatalError。如何通过我的 fetchChanges 方法处理公共数据库通知?

 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

    let dict = userInfo as! [String: NSObject]
    guard let vc = self.window?.rootViewController as? UIViewController else { return }
    guard let notification:CKDatabaseNotification = CKNotification(fromRemoteNotificationDictionary:dict) as? CKDatabaseNotification else { return }
    self.fetchChanges(in: notification.databaseScope) {
        completionHandler(UIBackgroundFetchResult.newData)
    }

}

func fetchChanges(in databaseScope: CKDatabaseScope, completion: @escaping () -> Void) {
    switch databaseScope {
    case .private:
        self.fetchPrivateChanges(completion: completion)
    case .shared:
        self.fetchSharedChanges(completion:) { status in
            if (status == false) {
                return
            }
        }
    case .public:
        fatalError()
    }
}
4

1 回答 1

0

您可以在您的声明中更改case .public为,因为如果它不是私有的或共享的,那么它必须是公开的。default:switch

另外,为了清楚起见,你不必fatalError()在那里。如果那是导致您悲伤的原因,请删除它并使用通知做一些事情。

我要做的另一件事是检查订阅 ID,didReceiveRemoteNotification以了解更多关于我应该如何处理它的信息。你可以像这样得到它:

if let sub = notification.subscriptionID{
  print(sub) //Prints the subscription ID
}

希望这会有所帮助。:)

于 2020-06-11T18:20:54.387 回答