Processing notifications from public, private and shared database in didReceiveRemoteNotification

119 views Asked by At

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()
    }
}
1

There are 1 answers

2
Clifton Labrum On

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. : )