我已经为一个特定的类创建了一个CloudKit订阅,该类会在对象创建或更新时发出推送通知。
该代码如下:
// Create the predicate
let predicate = NSPredicate(format: "recordId = %@", RECORD_ID)
// Create a subscription specifying the record type, predicate and notification options
let subscription = CKQuerySubscription(recordType: "Notes", predicate: predicate, options: [.firesOnRecordUpdate, .firesOnRecordCreation])
// Create a CloudKit notification object
let notificationInfo = CKNotificationInfo()
notificationInfo.shouldBadge = true
// Set the subscriptor's notification object to the new CloudKit notification object
subscription.notificationInfo = notificationInfo
// Save the subscription to the database
let publicDatabase = CKContainer.default().publicCloudDatabase
publicDatabase.save(subscription) { (subs, err) in
if let err = err {
print("Failed to save subscription:", err)
return
}
}订阅将成功地保存在服务器中。
我想要创建一个无声通知,并且我正在处理通知(当收到通知时)如下:
import UserNotifications
// Register for push notifications
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.requestAuthorization(options: [.badge, .alert, .sound]) { (success, err) in
if let err = err {
print("Failed to request oauth for notifications:", err)
}
}
application.registerForRemoteNotifications()
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("didRegisterForRemoteNotificationsWithDeviceToken:", deviceToken)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register notifications_ error:", error)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("------------------------")
print("Receiving updates from the server")
// Convert the userInfo parameter to a CKNotification object
let cloudKitNotification = CKNotification(fromRemoteNotificationDictionary: userInfo)
// Get the body of the notification
let alertBody = cloudKitNotification.alertBody
print("alertBody:", alertBody ?? "")
// Get the new or modified record form the CKQueryNotification object
if cloudKitNotification.notificationType == CKNotificationType.query {
guard let queryNotification = cloudKitNotification as? CKQueryNotification else {
print("could not cast the query notification")
return
}
let recordId = queryNotification.recordID
print("recordId:", recordId ?? "")
}
}问题出在这里。
如果我正在使用该应用程序,并且向服务器发送了一个更新,那么我的didReceiveRemoteNotification就会触发,而且一切都会正常工作,但是如果我的应用程序处于后台,当收到推送通知但didReceiveRemoteNotification没有启动时,我的app_notification徽章就会改变(增加1)。
我的问题是,当推送通知条目到达时,我如何处理它?
我已经用Required background modes和App downloads content in response to push notifications在Info.plis文件中设置了App downloads content from the network
还有一点我注意到了,如果我将notificationInfo.shouldBadge设置为false,则不会收到推送通知。
谢谢你的帮助
发布于 2017-06-15 05:10:25
通知不能保证到达,特别是当用户进入飞机模式,失去连接,等等,即使你收到通知,当你的应用程序运行时,你不能执行任何处理。这意味着,每次启动应用程序时,都必须假定仍有一些通知等待处理(那些在您不在应用程序时收到的通知以及那些由于任何原因从未收到任何推送通知的通知)。
因此,当您启动应用程序时,您需要发出一个CKFetchNotificationsChangesOperation并查找尚未处理的新通知。
创建CKFetchNotificationChangesOperation操作,并设置notificationChangedBlock。对于从服务器提取的每个通知,该块将被调用一次。您可以评估它是否已经标记为"read“。如果不是,那么无论您在didReceiveRemoteNotification中通常做什么,都要处理它。
另外,您还可以在fetchNotificationChangesCompletionBlock中设置代码,在从服务器获取所有通知之后,在操作结束时只触发一次。
https://stackoverflow.com/questions/44547210
复制相似问题