Search code examples
swiftcloudkitmac-catalystcksubscription

didReceiveRemoteNotification not being called on MacCatalyst


When I create a CKSubscription, didReceiveRemoteNotification gets called on iOS just fine but not on MacOS. I came across a 2015 SO thread talking about a bug and the suggested workaround was to set the notification info's soundName to an empty string - unfortunately that didn't resolve the issue for me.

Here is how I register my remote notifications:

  func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

        let subscription = CKQuerySubscription(recordType: "Reminder", predicate: NSPredicate(format: "TRUEPREDICATE"), options: [.firesOnRecordCreation, .firesOnRecordUpdate])

        // Here we customize the notification message
        let info = CKSubscription.NotificationInfo()

        info.shouldSendContentAvailable = true
        info.desiredKeys = ["identifier", "title", "date"]
        info.soundName = ""

        subscription.notificationInfo = info

        // Save the subscription to Private Database in Cloudkit
        CKContainer.default().privateCloudDatabase.save(subscription, completionHandler: { subscription, error in
            if error == nil {
                // Subscription saved successfully 
            } else {
                // Error occurred
            }
        })
    }

Solution

  • This has to do with the bundle identifier being different on Mac Catalyst. Thanks to the soon to be introduced universal app purchase, catalyst apps can now bear the same bundle identifier as their iOS counterpart, and that fixes the issue.

    Note that I was also experiencing issues with cloudkit key values not syncing on Mac (NSUbiquitousKeyValueStore). Having a single bundle id for Mac and iOS fixed the problem too.