CloudKit 订阅问题
CloudKit Subscription Woes
我正在为以下订阅而苦苦挣扎:
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 创建或更新他们的播放器记录并且在 10KM 以内时,通过推送通知用户 A。
我相信我在我的应用程序中正确设置了推送权限(例如,系统会提示用户在创建他们的 sub 之前确认这一点)。
没有推送到达。有任何想法吗?我是否对 CK 有一些基本的误解?
您似乎没有注册推送通知:
iOS Developer Library: Subscribing to Record Changes
Saving subscriptions to the database doesn’t automatically configure your app to receive notifications when a subscription fires. CloudKit uses the Apple Push Notification service (APNs) to send subscription notifications to your app, so your app needs to register for push notifications to receive them.
Go to AppDelegate.swift and put this code into the didFinishLaunchingWithOptions method:
let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
UIApplication.sharedApplication().registerForRemoteNotifications()
和
For the sake of completion, you could optionally also catch the didReceiveRemoteNotification message sent to your app delegate, which is called if a push message arrives while the app is running. Something like this ought to do the trick:
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)
}
}
}
}
我正在为以下订阅而苦苦挣扎:
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 创建或更新他们的播放器记录并且在 10KM 以内时,通过推送通知用户 A。
我相信我在我的应用程序中正确设置了推送权限(例如,系统会提示用户在创建他们的 sub 之前确认这一点)。
没有推送到达。有任何想法吗?我是否对 CK 有一些基本的误解?
您似乎没有注册推送通知:
iOS Developer Library: Subscribing to Record Changes
Saving subscriptions to the database doesn’t automatically configure your app to receive notifications when a subscription fires. CloudKit uses the Apple Push Notification service (APNs) to send subscription notifications to your app, so your app needs to register for push notifications to receive them.
Go to AppDelegate.swift and put this code into the didFinishLaunchingWithOptions method:
let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
UIApplication.sharedApplication().registerForRemoteNotifications()
和
For the sake of completion, you could optionally also catch the didReceiveRemoteNotification message sent to your app delegate, which is called if a push message arrives while the app is running. Something like this ought to do the trick:
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)
}
}
}
}