CloudKit Mac 上未调用 didReceiveRemoteNotification
CloudKit didReceiveRemoteNotification not called on the Mac
我正在使用以下 CKNotification 信息,这似乎工作正常:
CKNotificationInfo *note = [[CKNotificationInfo alloc] init];
note.alertBody = @"Something Happened";
note.shouldBadge = NO;
note.shouldSendContentAvailable = NO;
当 iOS 设备上发生某些变化时,我的 Mac 应用程序会收到一个推送通知,该通知基于带有此通知的订阅。但是,从未调用 didReceiveRemoteNotification
所以我无法处理该事件。我需要能够刷新并获取新的更改。我该怎么做?
调用 registerForRemoteNotificationTypes:
并实施 didRegisterForRemoteNotificationsWithDeviceToken:
代码应该足够了,App ID 应该包含 Push Notifications 服务。
我在 cross-platform (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 class:
中设置 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 中的处理方式如此 class:
/// @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);
}
}
好吧,我终于明白了。如果您将 CKNotificationInfo 用于警报,除非您将 CKNotificationInfo.soundName
设置为空字符串,否则不会在 Mac 上调用 didReceiveRemoteNotification
!这看起来像是 OS X(目前为 10.10 和 10.11)中的错误,但可以通过这个简单的更改来解决。
我正在使用以下 CKNotification 信息,这似乎工作正常:
CKNotificationInfo *note = [[CKNotificationInfo alloc] init];
note.alertBody = @"Something Happened";
note.shouldBadge = NO;
note.shouldSendContentAvailable = NO;
当 iOS 设备上发生某些变化时,我的 Mac 应用程序会收到一个推送通知,该通知基于带有此通知的订阅。但是,从未调用 didReceiveRemoteNotification
所以我无法处理该事件。我需要能够刷新并获取新的更改。我该怎么做?
调用 registerForRemoteNotificationTypes:
并实施 didRegisterForRemoteNotificationsWithDeviceToken:
代码应该足够了,App ID 应该包含 Push Notifications 服务。
我在 cross-platform (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 class:
- (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 中的处理方式如此 class:
/// @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);
}
}
好吧,我终于明白了。如果您将 CKNotificationInfo 用于警报,除非您将 CKNotificationInfo.soundName
设置为空字符串,否则不会在 Mac 上调用 didReceiveRemoteNotification
!这看起来像是 OS X(目前为 10.10 和 10.11)中的错误,但可以通过这个简单的更改来解决。