El Tomato
El Tomato

Reputation: 6707

CKQuerySubscription: Failing to Make Subscription

I have an iOS app that I'm working on with CloudKit. And I'm trying to make a subscription.

import CloudKit

class HomeViewController: UIViewController {
    override func viewDidLoad() {
        registerSubscription()
    }

    func registerSubscription() {
        let cloudContainer = CKContainer(identifier: "iCloud.com.xxx.XXXXX")
        let privateDB = cloudContainer.privateCloudDatabase
        let predicate = NSPredicate(format: "TRUEPREDICATE") // TRUEPREDICATE: all records of the specified type match the predicate
        let subscription = CKQuerySubscription(recordType: "PrivateRecords", predicate: predicate, options: .firesOnRecordCreation)
        privateDB.save(subscription, completionHandler: ({returnRecord, error in
            if let err = error {
                print("Subscription has failed: \(err.localizedDescription)")
            } else {
                print("Subscription set up successfully")
                print("Subscription ID: \(subscription.subscriptionID)")
            }
        }))
    }
}

And it will consistently go to the error. The error says the following.

Subscription with a nil notificationInfo: CKQuerySubscription: 0x7fd7bfca7ec0; ; Query Subscription: recordType=PrivateRecords, predicate=TRUEPREDICATE, subscriptionOptions=1, subscriptionID=728863F8-3852-4A12-B5C6-F65EC945207A, zoneID=(null)

I can delete the subscription based on the subscription ID. But it'll give me a different ID every time I run this app. I have actually done the same thing with a macOS application. And it has a subscription. Additionally, I deleted a subscription type about a week ago to see what happens. I don't really know what the subscription ID is for.

So what am I doing wrong in making a subscription? Thanks.

enter image description here

Upvotes: 0

Views: 728

Answers (2)

Elijah Ahmad
Elijah Ahmad

Reputation: 36

CKQuerySubscriptions have a property notificationInfo, what you'll need to do is something like the following:

let notification = CKSubscription.NotificationInfo()
notification.alertBody = "You have a new message"
notification.shouldSendContentAvailable = true

newSubscription.notificationInfo = notification

Upvotes: 2

rmaddy
rmaddy

Reputation: 318934

CKQuerySubscription extends CKSubscription. One of the properties you need to set before saving the subscription is notificationInfo. The message is pointing out that this property is nil and should be set to something useful.

Upvotes: 1

Related Questions