Search code examples
iosswiftcloudkit

CKDatabaseSubscription not pushing notifications


I'm trying to receive changes with CKDatabaseSubscription but I'm stuck that I don't receive any push notifications from that subscription.

I'm subscribing to changes in private record zone via such method:

let container = CKContainer.default()

func subscribeChanges() {
    let subscription = CKDatabaseSubscription(subscriptionID: "test")

    let notificationInfo = CKNotificationInfo()
    notificationInfo.shouldSendContentAvailable = true
    subscription.notificationInfo = notificationInfo

    let operation = CKModifySubscriptionsOperation(subscriptionsToSave: [subscription], subscriptionIDsToDelete: [])
    operation.modifySubscriptionsCompletionBlock = { savedSubscriptions, deletedSubscriptionIDs, operationError in
        if operationError != nil {
            print(operationError)
            return
        } else {
            print("Subscribed")
        }
    }

    container.privateCloudDatabase.add(operation)
}

Subscribe is successful. But I don't receive any notifications on device, I've checked with such method:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    application.registerForRemoteNotifications()
    return true
}

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

}

I'm testing on real device connected to XCode, tried to push changes via simulator and Apple's dashboard.

Update

As I've found CKDatabaseSubscription only works for shared record zones. For public & private zone I can use something like that and it works:

CKQuerySubscription(recordType: "TestRecordType", predicate: NSPredicate(format: "TRUEPREDICATE"), options: CKQuerySubscriptionOptions.firesOnRecordCreation)

Solution

  • As for know CKDatabaseSubscription will fire notifications only for record zones with CKRecordZoneCapabilityFetchChanges, all private custom zones have that capability (shared I think also), default zones not.

    So only way now is to create new custom zone.