CloudKit 订阅问题
Posted
技术标签:
【中文标题】CloudKit 订阅问题【英文标题】:CloudKit Subscription Woes 【发布时间】:2016-06-12 15:08:58 【问题描述】:我正在为以下订阅而苦苦挣扎:
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。
我相信我在我的应用中正确设置了推送权限(例如,在创建用户之前,会提示用户确认这一点)。
没有推送到达。有任何想法吗?我是否有一些基本的 CK 误解?
【问题讨论】:
能否在创建 CK 记录和注册通知的位置添加代码? distanceToLocation:fromLocation:() 返回米而不是公里。您需要通过 10000 而不是 10。 Diegog FTW!米似乎抓住了它!令人惊讶的是,网络上有关于此的错误信息。 :| 【参考方案1】:您似乎没有注册推送通知:
ios Developer Library: Subscribing to Record Changes
将订阅保存到数据库不会自动将您的应用配置为在订阅触发时接收通知。 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)
【讨论】:
我正在其他地方注册通知。我想知道的是,这种类型的订阅是否可以通知玩家 A 玩家 B 已将其在 CK 中的 Player 对象更新为距离其当前位置 10KM 以内的位置。在我的仪表板中,有两个帐户在两个测试设备上,我仍然只看到一个订阅类型。 奖励 didgog 以表彰他在米与公里之间的观察。以上是关于CloudKit 订阅问题的主要内容,如果未能解决你的问题,请参考以下文章