Handling the CloudKit Notification

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 {
            return
        }
        
        guard let qry = ck["qry"] as? [String: AnyObject] else {
            return
        }
        
        let recordID = qry["rid"] as! String
        
        print(recordID)
        
        let options = CKQuerySubscription.Options( rawValue: qry["fo"] as! UInt )
        switch options {
        case .firesOnRecordCreation:
            print("FIRE ON RECORD CREATION")
            break
        case .firesOnRecordDeletion:
            print("FIRE ON RECORD DELETE")
            break
        case .firesOnRecordUpdate:
            print("FIRE ON UPDATE")
            break
        default:
            print("DEFAULT \(options)")
        }
    }

Replies

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


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

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",
               @"TRIGGER_TIME",@"FAR",@"NOTICE_DATE",@"TRIGGER_DATE",@"COMMENTS1",nil];
        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.