首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CloudKit CKSubscription通知基础

CloudKit CKSubscription通知基础
EN

Stack Overflow用户
提问于 2014-11-12 14:50:28
回答 1查看 2.1K关注 0票数 4

我今天一直在和CKSubscription闲逛。我对此有几个疑问。

我的订阅方式如下:

代码语言:javascript
复制
-(void)addSubscriptions
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"TRUEPREDICATE"];

    CKSubscription *newRecordSub = [[CKSubscription alloc] initWithRecordType:_recordTypeName predicate:predicate options:CKSubscriptionOptionsFiresOnRecordCreation | CKSubscriptionOptionsFiresOnRecordUpdate | CKSubscriptionOptionsFiresOnRecordDeletion];

    CKNotificationInfo *noteInfo = [CKNotificationInfo new];
    noteInfo.alertBody = @"Update!";
    noteInfo.shouldBadge = YES;
    noteInfo.soundName = UILocalNotificationDefaultSoundName;
    newRecordSub.notificationInfo = noteInfo;

    CKContainer *container = [CKContainer defaultContainer];
    CKDatabase *privateDatabase = [container privateCloudDatabase];

    [privateDatabase saveSubscription:newRecordSub completionHandler:^(CKSubscription *subscription, NSError *error) {

    }];
}

问题1:

当创建我的订阅时,我只需要将它保存到我的容器一次?由于这被称为加班费应用程序运行,我收到一个错误,简单地说,重复订阅,所以应用程序的工作方式如预期,但我是不是应该以不同的方式,而不是每次运行?

接下来,我设置了用我的AppID接收通知并在我的AppDelegate中注册它们。

代码语言:javascript
复制
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
    [application registerUserNotificationSettings:settings];
    [application registerForRemoteNotifications];

最后,我将这些通知处理如下:

代码语言:javascript
复制
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    CKNotification *note = [CKNotification notificationFromRemoteNotificationDictionary:userInfo];

    if (note.notificationType == CKNotificationTypeQuery)
    {
        CKQueryNotification *queryNote = (CKQueryNotification *)note;
        CKRecordID *recordID = [queryNote recordID];
        [[NSNotificationCenter defaultCenter] postNotificationName:kCloudKitNewRecordFlightLog object:recordID];
    }
}

问题2:

为何我现在没有收到任何通知呢?当添加/删除、更新新记录时,我的所有代码都会运行,我的应用程序也会被更新。但是,没有显示声音、徽章或横幅通知用户。

问题3:

在接收云工具包通知时,让本地通知向相关视图发送消息以更新记录,这是一种明智的方法吗?

EN

回答 1

Stack Overflow用户

发布于 2014-11-12 14:57:38

  1. 是的,你只需要设置一次。您可以查询订阅(使用fetchAllSubscriptionsWithCompletionHandler)以查看是否应该创建订阅。或者忽略这个错误。
  2. 这可能有效,或者您可以设置一个委托机制。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26889786

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档