dsk1306
dsk1306

Reputation: 129

CloudKit subscription not working

I'm trying to subscribe to push notifications using CloudKit with Swift. Here is my code:

App delegate:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    //Push
    let settings = UIUserNotificationSettings(forTypes: .Alert, categories: nil)
    application.registerUserNotificationSettings(settings)
    application.registerForRemoteNotifications()
    if let options: NSDictionary = launchOptions {
        let remoteNotification = options.objectForKey(UIApplicationLaunchOptionsRemoteNotificationKey) as? NSDictionary
        if let notification = remoteNotification {
            self.application(application, didReceiveRemoteNotification: notification as! [NSObject : AnyObject])
        }
    }

    return true

}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    let ckNotification = CKNotification(fromRemoteNotificationDictionary: userInfo as! [String : NSObject])
    if ckNotification.notificationType == .Query {
        let queryNotification = ckNotification as! CKQueryNotification
        let recordID = queryNotification.recordID
        let container = CKContainer.defaultContainer()
        let privateDatabase = container.privateCloudDatabase
        privateDatabase.fetchRecordWithID(recordID!) {newRecord, error in
            if error != nil {
                print(error)
            } else {
                NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
                    print(newRecord)
                })
            }
        }
    }
}

Creation:

func addNewRecordsSub() {
    let subscription = CKSubscription(recordType: "UserRecords", predicate: predicate, options: .FiresOnRecordCreation)
    let notificationInfo = CKNotificationInfo()
    notificationInfo.alertBody = "OK!"
    notificationInfo.shouldBadge = true
    subscription.notificationInfo = notificationInfo
    let privateDatabase = container.privateCloudDatabase
    privateDatabase.saveSubscription(subscription) { subscription, error in
        if error != nil {
            print(error)
        }

    }
}

After launch subscription has appear in CloudKit's dashboard:

enter image description here

But nothing happens when I'm adding new record... Just nothing. Did I missed something?

Upvotes: 0

Views: 574

Answers (1)

dsk1306
dsk1306

Reputation: 129

I tried to reset environment once more and everything is work now...

Upvotes: 1

Related Questions