macos - Mac 上未调用 CloudKit didReceiveRemoteNotification

标签 macos cocoa icloud cloudkit osx-yosemite

我正在使用以下 CKNotification 信息,这似乎工作正常:

CKNotificationInfo *note = [[CKNotificationInfo alloc] init]; note.alertBody = @"发生了什么事"; 注意.shouldBadge = NO; note.shouldSendContentAvailable = NO;

当 iOS 设备上发生某些变化时,我的 Mac 应用程序会收到基于此通知的订阅的推送通知。但是,didReceiveRemoteNotification 从未被调用,因此我无法处理该事件。我需要能够刷新并获取新的更改。我该怎么做?

最佳答案

调用registerForRemoteNotificationTypes:并实现didRegisterForRemoteNotificationsWithDeviceToken: 应该有足够的代码,并且应用程序 ID 应包含推送通知服务。

我在跨平台 (iOS/OS X) 应用程序中使用 CloudKit 在设备之间同步收藏夹,如下所示:

// OS X specific code
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    [NSApp registerForRemoteNotificationTypes:NSRemoteNotificationTypeNone];// silent push notification!
}

- (void)application:(NSApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    [self.favCon handleCloudKitNotificationWithUserInfo:userInfo];
}

请注意 NSRemoteNotificationTypeNone 的用法,这意味着静默推送通知!这就是我在 FavController 类中设置 CloudKit 的方法:

- (void)getOrCreateFavZoneWithCompletionHandler:(successCompletionHandler)handler {

    // check if FavZone exists op
    __block int createZone = 0;
    CKFetchRecordZonesOperation *fetchRecZonesOp    = [[CKFetchRecordZonesOperation alloc] initWithRecordZoneIDs:@[[FavController favRecordZoneID]]];
    CKModifyRecordZonesOperation *saveRecZoneOp     = [[CKModifyRecordZonesOperation alloc] initWithRecordZonesToSave:nil recordZoneIDsToDelete:nil];
    fetchRecZonesOp.fetchRecordZonesCompletionBlock = ^(NSDictionary *recordZonesByZoneID, NSError *operationError) {
        if (recordZonesByZoneID.count == 0) {// zone doesn't exist
            createZone              = 1;
            CKRecordZone *favZone   = [[CKRecordZone alloc] initWithZoneName:UTXAFavZoneName];
            saveRecZoneOp.recordZonesToSave         = @[favZone];
            NSLog(@"Creating new Zone %@", favZone.zoneID.zoneName);
        } else {
            NSLog(@"Zone %@ already exists.", [FavController favRecordZoneID].zoneName);
        }
    };

    // create FavZone op
    saveRecZoneOp.modifyRecordZonesCompletionBlock  = ^(NSArray *savedRecordZones, NSArray *deletedRecordZoneIDs, NSError *operationError) {
        [self successCompletionHandler:(savedRecordZones.count == createZone) error:operationError informDelegate:YES handler:handler];
    };

    [saveRecZoneOp addDependency:fetchRecZonesOp];
    [[FavController favDatabase] addOperation:fetchRecZonesOp];
    [[FavController favDatabase] addOperation:saveRecZoneOp];
}

- (void)subscribeToFavChanges:(successCompletionHandler)handler {

    // get current subscription
    [[FavController favDatabase] fetchSubscriptionWithID:UTXAFavConCKSubscriptionID completionHandler:^(CKSubscription *subscription, NSError *error) {
        if (subscription) {
            NSLog(@"using existing subscription: %@", subscription);
            [self successCompletionHandler:YES error:nil informDelegate:NO handler:handler];
        } else {
            CKSubscription *sub = [[CKSubscription alloc] initWithZoneID:[FavController favRecordZoneID]
                                                          subscriptionID:UTXAFavConCKSubscriptionID
                                                                 options:0];// "You must specify 0 for this parameter. Zone subscriptions currently do not support any options."
            [[FavController favDatabase] saveSubscription:sub completionHandler:^(CKSubscription *subscription, NSError *error) {

                NSLog(@"created new subscription: %@ %@", subscription, error);
                [self successCompletionHandler:(error == nil) error:error informDelegate:YES handler:handler];
            }];
        }
    }];
}

一旦我在一台设备上添加或删除记录,我就会在所有其他设备上收到通知,我会像在 FavController 类中那样处理该通知:

/// @abstract Handle push notifications sent by iCloud.
/// @discussion App delegates call this method when they receive a push notification through didReceiveRemoteNotification.
///          Currently, only airport favorites produce a PN, it is of type CKNotificationTypeRecordZone.
/// @param userInfo The userInfo dict tied to each push notification.
- (void)handleCloudKitNotificationWithUserInfo:(NSDictionary *)userInfo {

        [self recursivelyCheckForPreviousCloudKitNotifications];
}


- (void)recursivelyCheckForPreviousCloudKitNotifications {

    CKFetchNotificationChangesOperation *fetchOp    = [[CKFetchNotificationChangesOperation alloc] initWithPreviousServerChangeToken:_defCon.notificationChangeToken];
    __weak CKFetchNotificationChangesOperation *weakOp = fetchOp;

    fetchOp.notificationChangedBlock                = ^(CKNotification *notification) {
            [self handleNotification:notification];
    };

    fetchOp.fetchNotificationChangesCompletionBlock = ^( CKServerChangeToken *serverChangeToken, NSError *operationError) {
        NSLog(@"new notification change token: %@", serverChangeToken);
        _defCon.notificationChangeToken = serverChangeToken;
        if (weakOp.moreComing) {
            NSLog(@"more coming!!");
            [self recursivelyCheckForPreviousCloudKitNotifications];
        } else {
            NSLog(@"done handling notification changes.");

        }
    };
    [[FavController favContainer] addOperation:fetchOp];
}



- (void)handleNotification:(CKNotification *)notification {// withCompletionHandler:(successCompletionHandler)handler {

    if (notification.notificationType == CKNotificationTypeRecordZone) {// make sure we handle only zone changes

        CKRecordZoneNotification *noti = (CKRecordZoneNotification *)notification;

        if ([noti.recordZoneID.zoneName isEqualToString:[FavController favRecordZoneID].zoneName]) {
            // received an update for the fav zone
            [self queuedFavUpdateFromCloud];
        } else {
            // received an update for an unknown zone
            NSLog(@"WARNING: received an update for an unknown zone: %@", noti.recordZoneID.zoneName);
        }
    } else {
        NSLog(@"WARNING: received unknown notification: %@", notification);
    }
}

关于macos - Mac 上未调用 CloudKit didReceiveRemoteNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32130206/

相关文章:

Python runtime_library_dirs 在 Mac 上不起作用

macos - Git 认为我是错误的用户

iphone - 可以在两个 iOS 设备之间发送数据吗?

objective-c - if 语句没有按我的预期工作

ios - 在应用程序之间共享 iCloud 文档

iphone - 手动同步 iCloud

objective-c - 渲染数学公式

没有返回类型的 Objective-c 方法

objective-c - 两个类之间的 IBOutlets 属性不保留。解除分配?

objective-c - 接收者类型 'WebFrame' 例如消息是前向声明