推送通知不会发送(或接收到)到使用CloudKit订阅的我的应用程序,因为有很多方法可以解决这个问题,所以我将在下面列出我所做的事情,这可能会帮助其他拥有同样的问题,(希望)会指出我在做错什么:(
具体来说,我试图在记录插入时(在背景中都在前台中)获得无提示通知,但我都没有。
项目设置我在功能下启用CloudKit,在仪表板中设置记录类型。我在功能下为应用程序启用了推送通知。而且我启用了“远程通知”背景模式。
注册启动时的远程通知:
// Register for push notification
let settings = UIUserNotificationSettings(forTypes: [.Alert,.Badge,.Sound], categories: nil)
application.registerUserNotificationSettings(settings)
// Register for remote notification
application.registerForRemoteNotifications()
监听推送通知注册成功(成功)并创建订阅:
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
NSLog("Remote notifications registered - %@",deviceToken)
createSubscriptions()
}
创建我的订阅对象,并设置其通知信息,然后将其保存到服务器:
private func createSubscriptions(user : CKRecord, completion: (NSError?) -> ()) {
let messageSubscription = CKSubscription(recordType: "Messages", predicate: NSPredicate(format:"TRUEPREDICATE"), subscriptionID: "Messages", options: .FiresOnRecordCreation)
let notificationInfo = CKNotificationInfo()
notificationInfo.shouldSendContentAvailable = true
notificationInfo.soundName = ""
messageSubscription.notificationInfo = notificationInfo
CKContainer.defaultContainer().publicCloudDatabase.saveSubscription(subscription) { (_, error) in
if let error = error {
if error.domain == "CKErrorDomain" && error.code == 15 {
NSLog("Already set, ignoring dupe")
}
else
{
completion(error)
return
}
}
completion(nil)
}
}
我可以在CloudKit仪表板中看到成功创建了订阅。
然后,我从第二个设备或仪表板创建了一些记录(“消息”),看到它们已添加到查询或仪表板调用中,但是从未调用过我必须获取通知的方法:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
// Crickets. Never called
}
我知道通知不适用于模拟器。我已经在使用设备的开发人员中以及在使用testflight和许多设备的生产中进行了尝试。我缺少明显的东西吗?
最佳答案
您似乎做对了所有事情。我能想到的两件事:
application.registerForRemoteNotifications()
的调用。我假设您忘记将其粘贴在这里。