我正在努力完成以下订阅:
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")
}
}
基本上,当创建或更新新的播放器记录时,我会记录用户的位置。
我希望用户B在用户B创建或更新其播放器记录时通过推送通知用户A,并且在10KM以内。
我相信我在我的应用中正确设置了推送权限(例如,在创建子包之前,系统会提示用户确认)。
没有推动到达。有任何想法吗?我是否患有一些基本的CK误解?
答案 0 :(得分:0)
您似乎没有注册推送通知:
iOS Developer Library: Subscribing to Record Changes
保存对数据库的订阅不会自动将您的应用配置为在订阅触发时接收通知。 CloudKit使用Apple推送通知服务(APN)向您的应用发送订阅通知,因此您的应用需要注册推送通知以接收它们。
转到AppDelegate.swift并将此代码放入didFinishLaunchingWithOptions方法中:
let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
UIApplication.sharedApplication().registerForRemoteNotifications()
和
为了完成,您还可以选择捕获发送到您的app委托的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)
}
}
}
}