简体   繁体   English

CloudKit在记录创建时接收多个通知

[英]CloudKit Receive Multiple Notification on Record Creation

I am adding subscription to a Cloud Kit record type with FireOnCreation. 我正在使用FireOnCreation添加对Cloud Kit记录类型的订阅。

In the appDelegate, I used the didReceiveRemoteNotificationWithCompletionHandler to catch the notification. 在appDelegate中,我使用didReceiveRemoteNotificationWithCompletionHandler来捕获通知。 The issue I am having is when a record is created matching the predicate, the didReceiveRemoteNotificationWithCompletionHandler would trigger multiple time. 我遇到的问题是当创建与谓词匹配的记录时, didReceiveRemoteNotificationWithCompletionHandler会触发多次。 On device A, it would trigger 3 times consistently and where on device B, it would trigger 6 times. 在设备A上,它将一致地触发3次,而在设备B上,它将触发6次。 I even tried to create a record on the Cloud DashBoard and it would still do the same thing. 我甚至试图在Cloud DashBoard上创建一个记录,它仍然可以做同样的事情。 So that tells be the issue is not with the record creation. 所以这说明问题不在于创建记录。 Any suggestion or hints would be of great help. 任何建议或提示都会有很大帮助。

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
}

func subscribeRecordChangesForCurrentUser(userRecordID: CKRecordID) {
    print("subscribeRecordChangesForCurrentUser \(userRecordID.recordName)")
    let userRef = CKReference(recordID: userRecordID, action: CKReferenceAction.None)
    let predicate = NSPredicate(format: "toUsers CONTAINS %@", userRef)
    let subscription = CKSubscription(recordType: "Track", predicate: predicate, options: [.FiresOnRecordCreation])

    let notificationInfo = CKNotificationInfo()
    notificationInfo.alertBody = "Created a new track."
    notificationInfo.shouldSendContentAvailable = true
    notificationInfo.soundName = UILocalNotificationDefaultSoundName

    subscription.notificationInfo = notificationInfo

    let publicDatabase = CKContainer.defaultContainer().publicCloudDatabase

    publicDatabase.saveSubscription(subscription) { (subscription: CKSubscription?, error: NSError?) -> Void in
        guard error == nil else {
            print(error?.localizedDescription)
            return
        }
        print("successfully subscript user")
    }
}

You might have multiple subscriptions on those devices. 您可能在这些设备上有多个订阅。 When you create a new subscription, delete the old ones. 创建新订阅时,请删除旧订阅。

Take a look at 看一眼

CKDatabase.fetchAllSubscriptionsWithCompletionHandler(_:)

For a quick and dirty fix, I believe you can remove the app from the device and reinstall it. 对于快速和脏的修复,我相信您可以从设备中删除该应用程序并重新安装它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM