CloudKit 订阅不起作用
Posted
技术标签:
【中文标题】CloudKit 订阅不起作用【英文标题】:CloudKit subscription not working 【发布时间】:2016-02-17 11:10:45 【问题描述】:我正在尝试使用 CloudKit 和 Swift 订阅推送通知。这是我的代码:
应用委托:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
//Push
let settings = UIUserNotificationSettings(forTypes: .Alert, categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
if let options: NSDictionary = launchOptions
let remoteNotification = options.objectForKey(UIApplicationLaunchOptionsRemoteNotificationKey) as? NSDictionary
if let notification = remoteNotification
self.application(application, didReceiveRemoteNotification: notification as! [NSObject : AnyObject])
return true
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
let ckNotification = CKNotification(fromRemoteNotificationDictionary: userInfo as! [String : NSObject])
if ckNotification.notificationType == .Query
let queryNotification = ckNotification as! CKQueryNotification
let recordID = queryNotification.recordID
let container = CKContainer.defaultContainer()
let privateDatabase = container.privateCloudDatabase
privateDatabase.fetchRecordWithID(recordID!) newRecord, error in
if error != nil
print(error)
else
NSOperationQueue.mainQueue().addOperationWithBlock( () -> Void in
print(newRecord)
)
创作:
func addNewRecordsSub()
let subscription = CKSubscription(recordType: "UserRecords", predicate: predicate, options: .FiresOnRecordCreation)
let notificationInfo = CKNotificationInfo()
notificationInfo.alertBody = "OK!"
notificationInfo.shouldBadge = true
subscription.notificationInfo = notificationInfo
let privateDatabase = container.privateCloudDatabase
privateDatabase.saveSubscription(subscription) subscription, error in
if error != nil
print(error)
在 CloudKit 的仪表板中出现启动订阅后:
但是当我添加新记录时什么都没有发生……什么都没有。我错过了什么吗?
【问题讨论】:
你有没有尝试实现desiredKeys & soundName,看看有什么不同?notificationInfo.desiredKeys = ["yourKey1", "yourKey2"]
& notificationInfo.soundName = UILocalNotificationDefaultSoundName
@Allen 仍然不起作用。
我也尝试过重置环境,但还是没有。
您是在同一台设备上创建新记录吗?你不会收到通知。您必须在其他设备或仪表板中创建新记录
【参考方案1】:
我尝试再次重置环境,现在一切正常...
【讨论】:
以上是关于CloudKit 订阅不起作用的主要内容,如果未能解决你的问题,请参考以下文章
CloudKit:删除 CKSubscription 不起作用