Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not receiving CloudKit push notifications

I'm writing a small app that uses CloudKit. For some reason the app doesn't receive any notifications when there's a new record matching the query. Has anyone been able to get this feature to work?

I create new records in the app, but also in the CloudKit dashboard. The record is very simple, with a single integer field.

Create a record:

 CKRecord *record = [[CKRecord alloc] initWithRecordType:kSISCloudKitRecordTypeTest];
 record[@"value"] = @1;
 [self.publicDatabase saveRecord:record completionHandler:^(CKRecord *record, NSError *error)
  {
       // this call succeeds, no error.
  }];

Register for notifications:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [application registerForRemoteNotifications];
}

Create a subscription:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"value = 1"];

CKSubscription *subscription = [[CKSubscription alloc]
                                initWithRecordType:kSISCloudKitRecordTypeTest
                                predicate:predicate
                                options:CKSubscriptionOptionsFiresOnRecordCreation];

CKNotificationInfo *notificationInfo = [CKNotificationInfo new];
notificationInfo.alertLocalizationKey = @"LOCAL_NOTIFICATION_KEY";
notificationInfo.soundName = @"Party.aiff";
notificationInfo.shouldBadge = YES;
subscription.notificationInfo = notificationInfo;

[self.publicDatabase saveSubscription:subscription
                    completionHandler:^(CKSubscription *subscription, NSError *error)
 {
     // this succeeds as well, at least the 1st time I run it.
     // on subsequent calls it returns an error "duplicate subscription", which is OK by me.
 }

After running the above code, and creating a new record in the dashboard, I expect this app delegate method to be called:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    CKNotification *cloudKitNotification = [CKNotification notificationFromRemoteNotificationDictionary:userInfo];
    NSLog(@"cloudKitNotification: %@", cloudKitNotification);
}

However, it never gets called.

like image 489
TotoroTotoro Avatar asked Jun 16 '14 17:06

TotoroTotoro


People also ask

How do I enable push notifications on iOS?

Go to Settings and tap Notifications. Select an app under Notification Style. Under Alerts, choose the alert style that you want. If you turn on Allow Notifications, choose when you want the notifications delivered—immediately or in the scheduled notification summary.

How do I turn on push notifications for Safari?

Enable push notifications for Safari in your webpage To enable push notifications, follow this general approach in your webpage: Ask the user for permission to send them push notifications. Provide a method for the user to grant permission with a gesture, such as a click or tap on a button.


1 Answers

I am now receiving the notifications since Beta 3:

{
    aps =     {
    };
    ck =     {
        ce = 2;
        cid = "iCloud.com.domain.App";
        nid = "0b3ae470-d2c0-4f35-a817-12a899ee5964";
        qry =         {
            dbs = 2;
            fo = 1;
            rid = 88aee11ca88d4ecc45bf57c898b360c8e7e3d8bb;
            zid = "_defaultZone";
            zoid = "_defaultOwner";
        };
    };
}

Also, there’s a shouldSendContentAvailable property on CKNotificationInfo that makes it possible to receive the notifications in background – which also appears to work now (as of Beta 4).

like image 166
zoul Avatar answered Oct 11 '22 16:10

zoul