2

The CKSubscription documentation says: When a record modification causes a subscription to fire, the server sends push notifications to all devices with that subscription except for the one that made the original change to the record.

Let's assume I have two devices: device 1 an iPad and device 2 an iPhone both logged in with the same iCloud accounts. Let's assume both devices subscribe for record updates for a certain record type.

My code looks like this (I've taken out some of the housekeeping stuff)

NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"fromUserRecordIDName == %@", _member.userRecordIDName];
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"fromUserRecordIDName == %@", notificationPublic = [[CKNotificationInfo alloc] init];
notificationPublic.shouldBadge =YES;
notificationPublic.desiredKeys = @[@"fromMemberNameText"];
notificationPublic.shouldSendContentAvailable = YES;


if ([[NSUserDefaults standardUserDefaults] objectForKey:@"TickleRecordUpdateSubscription"] == nil) {

    [cloudManager subscribe:@"TickleRecord" public: YES options:CKSubscriptionOptionsFiresOnRecordUpdate predicate:predicate2 notification:notificationPublic completionHandler:^(CKSubscription *subscription, NSError *operationError) {

      if (operationError) {

       NSLog(@"An error occured in %@: %@", NSStringFromSelector(_cmd), operationError);



      } else{
        [[NSUserDefaults standardUserDefaults] setObject:subscription.subscriptionID forKey:@"TickleRecordUpdateSubscription"];
        [[NSUserDefaults standardUserDefaults] setObject:@"TickleRecordUpdateSubscription"forKey:subscription.subscriptionID ];

      }
    }];

It seems to me that when one device updates a record, the other device should be notified. But that is not what is happening - there is no notification. In the WWDC 2014 Advanced iCloud video they talk about being notified when one device marks a notification as read, but I haven’t had any success with that either.

Has anyone been successful in notifying one device when another device signed into the same account updates a record it?

Neal
  • 21
  • 3

2 Answers2

1

I had the same problem. I got it working with this subscription:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"recipient == %@", [[CKReference alloc] initWithRecordID:self.userRecordId action:CKReferenceActionNone]];
CKSubscription *subscription = [[CKSubscription alloc] initWithRecordType:@"Message" predicate:predicate options:CKSubscriptionOptionsFiresOnRecordCreation];
subscription.notificationInfo = [CKNotificationInfo new];
subscription.notificationInfo.shouldSendContentAvailable = YES;
subscription.notificationInfo.soundName = UILocalNotificationDefaultSoundName;
subscription.notificationInfo.shouldBadge = YES;
CKDatabase *publicDatabase = [[CKContainer defaultContainer] publicCloudDatabase];
[publicDatabase saveSubscription:subscription completionHandler:^(CKSubscription *subscription, NSError *error) {...}];

With that I'm able to create a record from one device and get a notification on another signed into the same iCloud account.

Edit: I just realized you're asking about record updates, not creation, but maybe my answer can still help. One thing I found peculiar when I had this problem was I started receiving notifications after I set a value for the soundName property of CKNotificationInfo.

  • I did get it to work on create, but not on update, although I think there are other issues with both devices trying to update a record simultaneously. Working on that now – Neal Oct 22 '14 at 09:44
  • you have set `soundName`, do you hear anything when notification was received? http://stackoverflow.com/questions/32033589/cknotificationinfo-soundname-does-not-work – János Aug 17 '15 at 08:07
  • 1
    Setting both `shouldSendContentAvailable` and `soundName`/`shouldBadge` will throw an assertion error as its unsupported. `shouldSendContentAvailable` should be set to true if you want to send a silent push notification, I.e. The user doesn't see it (no banner/sound). – Luke Jun 26 '16 at 14:13
0

No, you have to use 2 different accounts. I would like to think this is a CloudKit bug instead of a missing feature. Now there is no way you could have both devices with the most up to dat data.

Edwin Vermeer
  • 13,017
  • 2
  • 34
  • 58