I had push notifications from CloudKit working and I'm afraid I've done something to break them. If anyone can see something I don't, please help.
When the app launches, I call setupSubscriptions()
, which has this code:
let predicate = NSPredicate(value: true)
let subscriptionID = "public-new-changes-deleted"
let subscription = CKQuerySubscription(recordType: recordType, predicate: predicate, subscriptionID: subscriptionID, options: [.firesOnRecordCreation, .firesOnRecordUpdate, .firesOnRecordDeletion])
let notificationInfo = CKSubscription.NotificationInfo()
notificationInfo.shouldSendContentAvailable = true
subscription.notificationInfo = notificationInfo
publicDB.save(subscription) { subscription, error in
if error != nil {
print("subscription was set up")
}
The setup message does fire. I've also tried making the notificationInfo CKQuerySubscription.NotificationInfo
, but there's no discernible difference whether it's that or CKSubscription
.
In my app delegate:
application.registerForRemoteNotifications()
I do get a message from application(_ application:, didRegisterForRemoteNotificationsWithDeviceToken:)
that the application has registered for notifications.
Then I have:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("application did receive remote notification")
}
Next I got to my CloudKit Dashboard and create, modify, or delete a record but nothing happens. I'd expect a message from didReceiveRemoteNotification
. but nothing. This was working earlier but I can't think of what I changed to break it.
I can create records there and query for them in the app, so I'm sure it's able to see them, but I can't get a push when they're altered.
Other stuff:
In my target's Capabilities tab:
- Background Fetch and Remote Notifications are both checked under Background Modes.
- iCloud is on and it's using the correct container -- I can do fetches just fine from CloudKit using the same
recordType
and publicDBCKDatabase
object. - Push Notifications are turned on and my entitlements file has a flag for "APS Environment" with a value of
development
.
On my Apple Developer account page, under the App ID, iCloud and Push Notifications both have green lights for both "Development" and "Distribution."
I can see in the CloudKit dashboard that the subscription types are created once the app's been run.
I'm testing on a device, not in the simulator.
I've tried:
- Changing whether I create the subscription before or after I register for notifications.
- Adding a message body, alert sound, and
shouldBadge
, and requesting notifications usingUNUserNotificationCenter
, and making the App Delegate aUNUserNotificationCenterDelegate
. I get the prompt when I first run the app but the notifications don't arrive. - Splitting the subscriptions up into one for
.firesOnRecordCreation
and one for update and delete. - Adding the subscriptions using a
CKModifySubscriptionsOperation
instead of the database'ssave
method.
Please let me know if you have any ideas. Thank you.