我通过将userInfo转换为CKDatabaseNotification来处理私有和共享数据库通知。但我确实收到了didReceiveRemoteNotification方法中的公共数据库通知,并且苹果模板代码没有显示如何处理它,它引发了一个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()
}
}发布于 2020-06-12 02:20:54
您可以在switch语句中将case .public更改为default:,因为如果它不是私有或共享的,那么它就必须是公共的。
此外,为了清楚起见,您不需要在其中包含fatalError()。如果这就是让你悲伤的原因,那就把它去掉,然后用通知来代替它。
我做的另一件事是检查didReceiveRemoteNotification中的订阅ID,以了解更多关于我应该如何处理它的信息。你可以像这样得到它:
if let sub = notification.subscriptionID{
print(sub) //Prints the subscription ID
}希望这能有所帮助。:)
https://stackoverflow.com/questions/62315532
复制相似问题