我正在使用以下CKNotification信息,这似乎很好:
CKNotificationInfo *note = [[CKNotificationInfo alloc] init]; note.alertBody = @"Something Happened"; note.shouldBadge = NO; note.shouldSendContentAvailable = NO;
当iOS设备上发生变化时,我的Mac应用程序会收到基于此通知的订阅的推送通知。但是,didReceiveRemoteNotification从未被调用过,因此我无法处理该事件。我需要能够刷新和获取新的更改。我该怎么做?
发布于 2015-09-19 14:45:01
好吧我终于弄明白了。如果您使用CKNotificationInfo作为警报,则除非将CKNotificationInfo.soundName设置为空字符串,否则不会在Mac上调用CKNotificationInfo.soundName!这看起来只是OS中的一个bug (到目前为止是10.10和10.11 ),但是可以通过这个简单的更改来解决。
发布于 2015-08-21 13:18:18
调用registerForRemoteNotificationTypes:和实现didRegisterForRemoteNotificationsWithDeviceToken:应该是足够的代码,App应该包括推送通知服务。
我在跨平台(iOS/OS )应用程序中使用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的用法,这意味着无声的推送通知!下面是我如何在CloudKit类中设置FavController:
- (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);
}
}https://stackoverflow.com/questions/32130206
复制相似问题