未从CloudKit订阅接收推送通知

时间:2015-03-12 11:40:02

标签: ios swift cloudkit

我没有收到我期望来自CloudKit订阅的推送通知。

这是我到目前为止所做的:

  • 启用CloudKit和远程通知功能。
  • 使用CloudKit信息中心创建“测试”记录类型。
  • 为相应的记录类型(Test)创建了一个订阅 我可以在CloudKit仪表板中看到。
  • 使用物理设备进行测试,该设备已登录iCloud和。{ 连接到互联网。
  • 设置应用委托以接收通知。
  • 通过CloudKit门户网站手动插入/更新/删除记录。

不幸的是,我从未收到任何推送通知。涉及的代码如下所示。从字面上看,这是一个全新的空白项目中唯一的代码。

    // 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
}

提前感谢任何提示或建议。我希望这不是一个错误,我在这里做错了......它应该'只是工作'!

干杯

1 个答案:

答案 0 :(得分:0)

确保

  1. 您已在App的“功能”标签中启用了除CloudKit之外的推送通知(以及背景模式,如果需要)。如果需要,可以从Developer Portal中找到推送证书(一个用于Dev,一个用于生产),下载并安装它们(通过双击它们);

  2. 您正在测试设备上的应用。 Apple没有推送到模拟器。