我将CKSubscription设置为"触发记录创建"一张桌子。当我需要更新CKSubscription的特定实例时,我确保在添加它之前先删除它。
但是现在每当我保存一个新的CKRecord时,我都会得到四个相同的推送通知实例。有没有人经历过这个?如果有,是否有针对此问题的解决方法?
答案 0 :(得分:1)
是的,您需要告诉它您使用这样的代码处理通知吗?
func fetchNotificationChanges() {
let operation = CKFetchNotificationChangesOperation(previousServerChangeToken: nil)
var notificationIDsToMarkRead = [CKNotificationID]()
operation.notificationChangedBlock = { (notification: CKNotification) -> Void in
// Process each notification received
if notification.notificationType == .query {
let queryNotification = notification as! CKQueryNotification
let reason = queryNotification.queryNotificationReason
let recordID = queryNotification.recordID
print("reason \(reason)")
print("recordID \(recordID)")
// Do your process here depending on the reason of the change
// Add the notification id to the array of processed notifications to mark them as read
notificationIDsToMarkRead.append(queryNotification.notificationID!)
}
}
operation.fetchNotificationChangesCompletionBlock = { (serverChangeToken: CKServerChangeToken?, operationError: Error?) -> Void in
guard operationError == nil else {
// Handle the error here
return
}
// Mark the notifications as read to avoid processing them again
let markOperation = CKMarkNotificationsReadOperation(notificationIDsToMarkRead: notificationIDsToMarkRead)
markOperation.markNotificationsReadCompletionBlock = { (notificationIDsMarkedRead: [CKNotificationID]?, operationError: NSError?) -> Void in
guard operationError == nil else {
// Handle the error here
return
}
}
let operationQueue = OperationQueue()
operationQueue.addOperation(markOperation)
}
let operationQueue = OperationQueue()
operationQueue.addOperation(operation)
}
}