2 Replies
      Latest reply on Feb 10, 2019 4:54 AM by PBK
      mstoth Level 1 Level 1 (0 points)

        Well I finally got the CloudKit notification working after much effort.

        Perhaps my ignorance in dealing with the result is the problem but

        it seems to be a very cumbersome approach.


        One issue is when I create a record on the iPhone, the OS X app gets

        the expected .firesOnRecordCreation option.  But when I delete the record, I get the

        'or' of .firesOnRecordCreation and .firesOnRecordUpdate (the value 3)


        I present all 3 options when I create the subscription


        let qSubscription = CKQuerySubscription(recordType: "Student", predicate: predicate, subscriptionID: "test",

                                                        options: [.firesOnRecordCreation,.firesOnRecordUpdate, .firesOnRecordDeletion])


        I also set shouldSendMutableContent to true in the notificationInfo object

        however I don't get the record fields, I just get the record ID.  What mutable

        content am I supposed to get?


        The other issue is how cumbersome the code appears.  Is this really the way to use the userInfo object that is returned?

        Seems like there must be a more elegant way.


        Any tips would be appreciated.  Thanks!


        Here's the code (On the OS X app).


            func application(_ application: NSApplication, didReceiveRemoteNotification userInfo: [String : Any]) {
                guard let ck = userInfo["ck"] as? [String: AnyObject] else {
                guard let qry = ck["qry"] as? [String: AnyObject] else {
                let recordID = qry["rid"] as! String
                let options = CKQuerySubscription.Options( rawValue: qry["fo"] as! UInt )
                switch options {
                case .firesOnRecordCreation:
                    print("FIRE ON RECORD CREATION")
                case .firesOnRecordDeletion:
                    print("FIRE ON RECORD DELETE")
                case .firesOnRecordUpdate:
                    print("FIRE ON UPDATE")
                    print("DEFAULT \(options)")
        • Re: Handling the CloudKit Notification
          digitalglen Level 1 Level 1 (20 points)

          Be sure to set the desiredKeys attribute on the NotificationInfo object:


              notificationInfo.desiredKeys = ["uid", "name", etc]

          • Re: Handling the CloudKit Notification
            PBK Level 7 Level 7 (3,605 points)

            1) I'm not sure what the issue is with your comment

                  "But when I delete the record, I get the

            'or' of .firesOnRecordCreation and .firesOnRecordUpdate (the value 3)


            I do recall that when I created a subscription years ago I only got a call when I created a record.  There was no response, back then, to record update (or record delete)


            2) This works for me:

                    CKNotificationInfo *notification = [CKNotificationInfo new];
                    notification.alertBody=@"Event: %1$@   Type: %2$@\nTime: %3$@   FAR: %4$@\nComment: %7$@";
                    notification.alertLocalizationArgs=[NSArray arrayWithObjects:@"TRIGGER_NUM",@"NOTICE_TYPE",
                    notification.alertLocalizationKey = @" ";


                  and then in the AppDelegate:


            - (void)userNotificationCenter:(UNUserNotificationCenter *)center
            didReceiveNotificationResponse:(UNNotificationResponse *)response
                     withCompletionHandler:(void (^)(void))completionHandler{
                NSDictionary *userInfo=response.notification.request.content.userInfo;
                NSArray *theInfo=[[[userInfo objectForKey:@"aps"] objectForKey:@"alert"] objectForKey:@"loc-args"];
                    NSString *triggerTime=[theInfo objectAtIndex:2];
                     //  etc.