Search code examples
cloudkitsubscription

Defining Subscription IDs for cloud kit remote push notifications


I have about 12 CKRecord entities and assuming I store information in both public and private databases and also share from private database, I have to define at least 3 subscription IDs one for each CKRecord which means 36 subscription IDs. I can do this in individual controllers and in each controller I can handle remote push notifications recevied form AppDelegate.

In another method I can define 3 subscriptions one for each type of database and when the push notification arrives, for each zone, peek inside each record and based on each record type handle subscription. Does all this code need to be defined in AppDelegate?

Which is the better method and what are the advantages disadvantages of each. I would like to know before I spend time lot of time picking on one method and then having to backtrack because it was not right? Or do I need both methods to serve different purposes


Solution

  • I typically use the second method you mention. I use the CloudKit subscription ID to determine which method I should call from the AppDelegate and then have code elsewhere that processes the notification appropriately.

    Something like this:

    //MARK: Background & Push Notifications
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
      
      let dict = userInfo as! [String: NSObject]
      
      if let notification = CKNotification(fromRemoteNotificationDictionary: dict), let subscriptionID = notification.subscriptionID{
        print("iOS Notification: \(subscriptionID)")
        switch subscriptionID{
          case "a":
            processNotificationA(notification: notification)
          case "b":
            processNotificationB(notification: notification)
          default:
            processAnyNotification(notification: notification)
        }
      }
    }