CloudKit 订阅无效
CloudKit subscription not working
我正在尝试使用 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 的仪表板中:
但是当我添加新记录时没有任何反应...只是没有。我错过了什么吗?
我再次尝试重置环境,现在一切正常...
我正在尝试使用 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 的仪表板中:
但是当我添加新记录时没有任何反应...只是没有。我错过了什么吗?
我再次尝试重置环境,现在一切正常...