Cloudkit 没有发送推送通知

Cloudkit is not sending push notifications

推送通知没有发送(或接收)到我使用 CloudKit 订阅的应用程序,因为有很多方法可以解决这个问题,我将在下面列出我的内容已经完成,这可能会帮助其他人遇到同样的问题,并且(希望)会指出我做错了什么:(

具体来说,我试图在记录插入时获得静默通知(两者都在后台和前台),但我两者都没有。

项目设置我在功能下启用CloudKit,在仪表板中设置记录类型。我在功能下为应用程序启用推送通知。我启用背景模式 "remote-notification".

在启动时注册远程通知:

    // Register for push notification
    let settings = UIUserNotificationSettings(forTypes: [.Alert,.Badge,.Sound], categories: nil)
    application.registerUserNotificationSettings(settings)

    // Register for remote notification
    application.registerForRemoteNotifications()

监听推送通知注册成功(成功)并创建订阅:

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    NSLog("Remote notifications registered - %@",deviceToken)
    createSubscriptions()
}

创建我的订阅对象,设置它的通知信息,并将它保存到服务器:

private func createSubscriptions(user : CKRecord, completion: (NSError?) -> ()) {
        let messageSubscription = CKSubscription(recordType: "Messages", predicate: NSPredicate(format:"TRUEPREDICATE"), subscriptionID: "Messages", options: .FiresOnRecordCreation)
        let notificationInfo = CKNotificationInfo()
        notificationInfo.shouldSendContentAvailable = true
        notificationInfo.soundName = ""
        messageSubscription.notificationInfo = notificationInfo
        CKContainer.defaultContainer().publicCloudDatabase.saveSubscription(subscription) { (_, error) in
            if let error = error {
                if error.domain == "CKErrorDomain" && error.code == 15 {
                    NSLog("Already set, ignoring dupe")
                }
                else
                {
                    completion(error)
                    return
                }
            }
            completion(nil)
        }
}

我可以在 CloudKit 仪表板中看到订阅已成功创建。

然后我从第二个设备或仪表板创建一些记录 ("Message"),看到它们是在查询或仪表板调用中添加的,但从未调用我必须获取通知的方法:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    // Crickets.  Never called
}

我知道通知在模拟器上不起作用。我已经在使用设备的开发中尝试过,并在使用 testflight 和许多设备的生产中尝试过。我是否缺少明显的 osmething?

你似乎什么都做对了。我能想到的两件事:

  1. 您确定要注册远程通知吗?我在任何地方都看不到对 application.registerForRemoteNotifications() 的调用。我假设你忘了把它粘贴在这里。

  2. 您是否尝试过删除面板中的订阅并重新创建?我遇到过使用不同的 notificationInfo 多次注册同一个订阅会 中断 它并且它会停止触发的问题。

这有效...并且与您的版本略有不同...我不尝试注册特定通知,我注册所有...然后在收到后更仔细地查看它们他们。

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

    application.registerForRemoteNotifications()
    return true
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    let notification = CKQueryNotification(fromRemoteNotificationDictionary: userInfo as! [String : NSObject])


    if notification.notificationType == .Query {
        let queryNotification = notification
        if queryNotification.queryNotificationReason  == .RecordUpdated {
           print("queryNotification.recordID \(queryNotification.recordID)")
        }
    }
   }