How to set up a minimal CKSubscription?

前端 未结 2 415
别那么骄傲
别那么骄傲 2021-02-06 00:15

I want to set up a simple CKSubscription that notifies me a recordType was created, how?

2条回答
  •  囚心锁ツ
    2021-02-06 00:24

    After experimenting a while this is how to setup a minimal CKSubscription. You have to test it on Device, push notification does not work on simulator. You can add record in Dashboard, that will trigger push notification too.

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
    
        application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert, categories: nil))
        application.registerForRemoteNotifications()
            return true
    }
    
    func someFunc() { // <- you need to call once in app life time, and again when it was removed / installed 
         let defaultContainer = CKContainer.defaultContainer()
         let publicDatabase = defaultContainer.publicCloudDatabase   
         let subs = CKSubscription(recordType: "xxx", predicate: NSPredicate(value: true), subscriptionID: "yyy", options: .FiresOnRecordCreation)
         subs.notificationInfo = CKNotificationInfo()
         subs.notificationInfo.alertBody = "New item added"
         publicDatabase.saveSubscription(subs, completionHandler: {subscription, error in})
    }
    
    func application(application: UIApplication!, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]!)
    {
        // <- this method will invoked
    }
    

提交回复
热议问题