Important note: The question is not about simulating notifications, it's about receiving real notifications from CloudKit server and get working sync.
Notifications on Simulator never fires, even when app is launched, I just cannot test my app well. Specifically I've subscribed on CKDatabaseSubscription. Anyone knows how to allow notifications on Simulator to get working sync?
I receive notifications only on real device and have working sync, it works perfect there.
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 notifications will not work most of the time on simulator, you should test on real device.