Timbo
Timbo

Reputation: 1214

Not receiving Push Notifications from CloudKit Subscriptions

I'm not receiving Push Notifications I expect from CloudKit Subscriptions.

Here's what I've done so far:

Unfortunately I never receive any push notifications, ever. The code involved is shown below. Literally, this is the only code in a brand new blank project.

    // MARK: - SUBSCRIPTIONS
func subscribeToRecordChangesWithRecordType (recordType:String, database:CKDatabase) {

    let predicate = NSPredicate(value: true)
    let subscription = CKSubscription(recordType: recordType, predicate: predicate, options: CKSubscriptionOptions.FiresOnRecordCreation|CKSubscriptionOptions.FiresOnRecordDeletion|CKSubscriptionOptions.FiresOnRecordUpdate)

    database.saveSubscription(subscription, completionHandler: { (savedSubscription, error) -> Void in
        if let _error = error {
            NSLog("ERROR saving '%@' subscription %@",recordType, _error)
        } else {
            NSLog("SUCCESS creating '%@' subscription: %@", recordType, savedSubscription)
        }
    })
}
func createSubscriptions () {
    let privateDB = CKContainer.defaultContainer().privateCloudDatabase
    let publicDB = CKContainer.defaultContainer().publicCloudDatabase


    // NOTE: create a Record Type called 'Test' in the CloudKit dashboard
    self.subscribeToRecordChangesWithRecordType("Test", database: privateDB)
    self.subscribeToRecordChangesWithRecordType("Test", database: publicDB)
}

// MARK: - PUSH NOTIFICATIONS
func registerForPushNotifications (application: UIApplication) {
    self.createSubscriptions()
    let settings = UIUserNotificationSettings(forTypes: .Alert, categories: nil)
    application.registerUserNotificationSettings(settings)
    application.registerForRemoteNotifications()
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    NSLog("Registered for Push Notifications with token: %@", deviceToken);
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    NSLog("FAILED to register for Push Notifications. %@", error)
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    NSLog("RECEIVED Push Notification")
    NSNotificationCenter.defaultCenter().postNotificationName("PushNotificationReceived", object: userInfo)
}
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
    NSLog("RECEIVED LOCAL Push Notification")
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {

    NSLog("RECEIVED Push Notification with fetchCompletionHandler")
    NSNotificationCenter.defaultCenter().postNotificationName("PushNotificationReceived", object: userInfo)
}

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

    self.registerForPushNotifications(application)
    return true
}

Thanks in advance for any tips or suggestions. I hope this isn't a bug and that I'm doing something wrong here ... it should 'just work'!

Cheers

Upvotes: 4

Views: 1071

Answers (1)

CodeBrew
CodeBrew

Reputation: 7187

Make sure

  1. You have enabled Push Notification besides CloudKit (and Background Mode if needed) in App's Capabilities tab. And if needed, find the push certificates (one for Dev, one for production) from Developer Portal, download them and install them (by double clicking on them);

  2. You're testing the app on a device. Apple does not push to the simulator.

Upvotes: 0

Related Questions