【问题标题】:Not receiving Push Notifications from CloudKit Subscriptions未收到来自 CloudKit 订阅的推送通知
【发布时间】:2015-03-12 11:40:01
【问题描述】:

我没有收到来自CloudKit Subscriptions 的推送通知。

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

  • 启用CloudKit 和远程通知功能。
  • 使用 CloudKit 仪表板创建了一个“测试”记录类型。
  • 为适当的记录类型(测试)创建了订阅,它 我可以在 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
}

提前感谢您提供的任何提示或建议。我希望这不是一个错误,并且我在这里做错了什么......它应该“正常工作”!

干杯

【问题讨论】:

  • 代码看起来没问题。您是否能够查询您的公共数据库以进行 recordType 测试?你能告诉我们 NSLog 语句的输出是什么吗?您可以尝试在创建订阅之前执行 registerForRemoteNotifications 吗?
  • 一年,现在回答:(我遇到了完全相同的问题。令人惊讶的是,除了 Apple 提供的手册之外,关于这个主题的在线内容很少。
  • 我最终自己完成了同步逻辑。查看 timroadley dot com 的首页以下载代码。这是ezcloudkit.zip 项目
  • 我遇到了同样的问题 - 它似乎没有收到通知。我想知道它是一个错误还是一个功能?

标签: ios swift cloudkit


【解决方案1】:

确定

  1. 您已在应用程序的功能选项卡中启用了除 CloudKit 之外的推送通知(如果需要,还启用了后台模式)。如果需要,从 Developer Portal 中找到推送证书(一个用于开发,一个用于生产),下载并安装它们(通过双击它们);

  2. 您正在设备上测试应用程序。 Apple 不会推送到模拟器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-17
    • 2015-05-07
    相关资源
    最近更新 更多