我没有收到我期望来自CloudKit
订阅的推送通知。
这是我到目前为止所做的:
CloudKit
和远程通知功能。iCloud
和。{
连接到互联网。CloudKit
门户网站手动插入/更新/删除记录。不幸的是,我从未收到任何推送通知。涉及的代码如下所示。从字面上看,这是一个全新的空白项目中唯一的代码。
// MARK: - SUBSCRIPTIONS
func subscribeToRecordChangesWithRecordType (recordType:String, database:CKDatabase) {
let predicate = NSPredicate(value: true)
let subscription = CKSubscription(recordType: recordType, predicate: predicate, options: CKSubscriptionOptions.FiresOnRecordCreation|CKSubscriptionOptions.FiresOnRecordDeletion|CKSubscriptionOptions.FiresOnRecordUpdate)
database.saveSubscription(subscription, completionHandler: { (savedSubscription, error) -> Void in
if let _error = error {
NSLog("ERROR saving '%@' subscription %@",recordType, _error)
} else {
NSLog("SUCCESS creating '%@' subscription: %@", recordType, savedSubscription)
}
})
}
func createSubscriptions () {
let privateDB = CKContainer.defaultContainer().privateCloudDatabase
let publicDB = CKContainer.defaultContainer().publicCloudDatabase
// NOTE: create a Record Type called 'Test' in the CloudKit dashboard
self.subscribeToRecordChangesWithRecordType("Test", database: privateDB)
self.subscribeToRecordChangesWithRecordType("Test", database: publicDB)
}
// MARK: - PUSH NOTIFICATIONS
func registerForPushNotifications (application: UIApplication) {
self.createSubscriptions()
let settings = UIUserNotificationSettings(forTypes: .Alert, categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
NSLog("Registered for Push Notifications with token: %@", deviceToken);
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
NSLog("FAILED to register for Push Notifications. %@", error)
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
NSLog("RECEIVED Push Notification")
NSNotificationCenter.defaultCenter().postNotificationName("PushNotificationReceived", object: userInfo)
}
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
NSLog("RECEIVED LOCAL Push Notification")
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
NSLog("RECEIVED Push Notification with fetchCompletionHandler")
NSNotificationCenter.defaultCenter().postNotificationName("PushNotificationReceived", object: userInfo)
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.registerForPushNotifications(application)
return true
}
提前感谢任何提示或建议。我希望这不是一个错误,我在这里做错了......它应该'只是工作'!
干杯
答案 0 :(得分:0)
确保
您已在App的“功能”标签中启用了除CloudKit之外的推送通知(以及背景模式,如果需要)。如果需要,可以从Developer Portal中找到推送证书(一个用于Dev,一个用于生产),下载并安装它们(通过双击它们);
您正在测试设备上的应用。 Apple没有推送到模拟器。