未收到CloudKit推送通知

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了未收到CloudKit推送通知相关的知识,希望对你有一定的参考价值。

我正在编写一个使用CloudKit的小应用程序。出于某种原因,当有与查询匹配的新记录时,应用程序不会收到任何通知。有没有人能够使这个功能工作?

我在应用程序中创建了新记录,但也在CloudKit仪表板中创建了新记录。记录非常简单,只有一个整数字段。

创建记录:

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

注册通知:

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

创建订阅:

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.
 }

运行上面的代码并在仪表板中创建新记录后,我希望调用此app委托方法:

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

但是,它永远不会被调用。

答案

我现在收到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";
        };
    };
}

此外,在shouldSendContentAvailable上有一个CKNotificationInfo属性,可以在后台接收通知 - 现在似乎也可以使用(从Beta 4开始)。

以上是关于未收到CloudKit推送通知的主要内容,如果未能解决你的问题,请参考以下文章

iOS 未收到来自 CloudKit 的推送通知

来自 CloudKit 的推送通知未正确同步

CloudKit 推送通知订阅不起作用

CloudKit 谓词

Objective C-推送通知不适用于 Cloudkit

CloudKit 通知未发送到所有设备