在didReceiveRemoteNotification中处理来自公共、私有和共享数据库的通知。

问题描述 投票:1回答:1

我通过将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()
    }
}
swift cloudkit cknotification
1个回答
0
投票

你可以直接更改 case .publicdefault: 在你 switch 语句,因为如果它不是私有或共享的,那么它一定是公开的。

另外,我想说明一下,你不一定要有 fatalError() 在那里。如果这就是导致你痛苦的原因,那就把它删除,改用通知做一些事情。

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

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

希望能帮到你 : )

© www.soinside.com 2019 - 2024. All rights reserved.