iOS 模拟器从不接收来自 CloudKit 服务器的通知

iOS Simulator never receives notifications from CloudKit server

重要说明:问题不是关于模拟通知,而是关于从 CloudKit 服务器接收真实通知并进行工作同步。

模拟器上的通知从不触发,即使在启动应用程序时,我也无法很好地测试我的应用程序。具体来说,我订阅了 CKDatabaseSubscription。任何人都知道如何允许模拟器上的通知同步工作?

我只在真实设备上接收通知并且有工作同步,在那里工作完美。

func subscriptionToNotifications(for container: CKContainer) {
    if subscriptionIsLocallyCached { return }
    
    let application = UIApplication.shared
    let sharedDatabase = container.sharedCloudDatabase
    
    let subscription = CKDatabaseSubscription(subscriptionID: "shared-changes")
    subscription.recordType = "CD_List"
    
    let notificationInfo = CKSubscription.NotificationInfo()
    notificationInfo.shouldSendContentAvailable = true
    notificationInfo.shouldBadge = true
    notificationInfo.alertBody = "Task list was changed"
    notificationInfo.soundName = "default"
    
    subscription.notificationInfo = notificationInfo;
    
    /// Saving subscription to Shared Database
    container.sharedCloudDatabase.save(subscription) { (subscription, error) in
        if let error = error {
            print("Error saving subscription:\n", error.localizedDescription)
        } else if let subscription = subscription {
            print("Successfully saved Subscription:\n", subscription.debugDescription)
        }
    }
    
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
        if let error = error {
            print("Error registering notifications authorization")
        } else if granted {
            print("Successfully authorized Notifications!")
        }
    }
    
    application.registerForRemoteNotifications()
    
    /// Creating operation which will notify when subscription will be saved
    let operation = CKModifySubscriptionsOperation(subscriptionsToSave: [subscription], subscriptionIDsToDelete: [])
    
    operation.modifySubscriptionsCompletionBlock = { [unowned self] subscriptions, subscriptionIDs, error in
        if let error = error {
            print("Error registering notifications authorization")
        } else {
            self.subscriptionIsLocallyCached = true
            print("Successfully modified subscriptions")
        }
    }
    
    operation.qualityOfService = .utility
    
    sharedDatabase.add(operation)
}

CloudKit通知在大多数情况下在模拟器上不起作用,您应该在真实设备上进行测试。