2

I process both private and shared database notifications by converting userInfo to CKDatabaseNotification. But I do get public database notifications also in didReceiveRemoteNotification method and Apple template code does not show how to process it and it raises a fatalError. How can I process public database notifications via my fetchChanges method?

 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()
    }
}
vrao
  • 545
  • 2
  • 12
  • 33

1 Answers1

0

You can just change case .public to default: in your switch statement since if it's not private or shared, then it must be public.

Also, just to be clear, you don't have to have fatalError() in there. If that's what's causing you grief, then remove that and do something with the notification instead.

Another thing I do is check the subscription ID in didReceiveRemoteNotification to know more about what I should do with it. You can get it like this:

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

Hopefully that helps. : )

Clifton Labrum
  • 13,053
  • 9
  • 65
  • 128
  • The issue is if it is public database it would not call fetchChanges since I had a return stateent. – vrao Jun 14 '20 at 17:22
  • I changed the code to handle public db like this: if let notification:CKDatabaseNotification = CKNotification(fromRemoteNotificationDictionary:dict) as? CKDatabaseNotification { self.fetchChanges(in: notification.databaseScope) { completionHandler(UIBackgroundFetchResult.newData) } } else { if let notification:CKQueryNotification = CKNotification(fromRemoteNotificationDictionary:dict) as? CKQueryNotification { self.processPublicDBChanges(ckqn: notification) } } – vrao Jun 14 '20 at 17:26