2

我正在为以下订阅而苦苦挣扎:

let predicate = NSPredicate(format: "gc_alias != %@ AND distanceToLocation:fromLocation:(%K,%@) < %f",
                                self.localPlayer!.alias!,
                                "location",
                                self.currentLocation!,
                                10)


    let subscription = CKSubscription(recordType: "Player", predicate: predicate, options: .FiresOnRecordCreation)
    subscription.zoneID = nil

    let notification = CKNotificationInfo()
    notification.alertBody = "Nearby Player within Range!"
    notification.soundName = UILocalNotificationDefaultSoundName

    subscription.notificationInfo = notification

    let container = CKContainer.defaultContainer()
    let publicDb = container.publicCloudDatabase

    publicDb.saveSubscription(subscription) { (result, error) -> Void in
        if error != nil {
            print(error!.localizedDescription)
        } else {
            print("SUBSCRIBED SUCCESS")
            print(result)
            NSUserDefaults.standardUserDefaults().setBool(true, forKey: "subscribed")
        }
    }

基本上,当创建或更新新的 Player 记录时,我会记录用户的位置。

当用户 B 创建或更新他们的玩家记录并且在 10KM 内时,我希望通过推送通知用户 A。

我相信我在我的应用程序中正确设置了推送权限(例如,在创建他们的子之前提示用户确认这一点)。

没有推送到达。有任何想法吗?我是否有一些基本的 CK 误解?

4

1 回答 1

1

您似乎没有注册推送通知:

iOS 开发者库:订阅记录更改

将订阅保存到数据库不会自动将您的应用配置为在订阅触发时接收通知。CloudKit 使用 Apple 推送通知服务 (APN) 向您的应用发送订阅通知,因此您的应用需要注册推送通知才能接收它们。

根据Hacking with Swift: Delivering notifications with CloudKit push messages: CKSubscription and saveSubscription你应该:

转到 AppDelegate.swift 并将此代码放入 didFinishLaunchingWithOptions 方法中:

let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
UIApplication.sharedApplication().registerForRemoteNotifications()

为了完成,您还可以选择捕获发送到您的应用程序委托的 didReceiveRemoteNotification 消息,如果在应用程序运行时推送消息到达,则会调用该消息。这样的事情应该可以解决问题:

    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    if let pushInfo = userInfo as? [String: NSObject] {
        let notification = CKNotification(fromRemoteNotificationDictionary: pushInfo)

        let ac = UIAlertController(title: "What's that Whistle?", message: notification.alertBody, preferredStyle: .Alert)
        ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))

        if let nc = window?.rootViewController as? UINavigationController {
            if let vc = nc.visibleViewController {
                vc.presentViewController(ac, animated: true, completion: nil)
            }
        }
    }
}
于 2016-06-17T17:32:33.677 回答