当前位置: 动力学知识库 > 问答 > 编程问答 >

NSNotificationCenter defaultCenter iOS 8. Notification is not being delivered to one of observers

问题描述:

Prior iOS 8 all works fine. The problem is:

I have two observers in different classes:

class1:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinishParseUser:)

name:USERS_LOADED_NOTIFICATION_ID object:nil];

class2:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinishParseUser:)

name:USERS_LOADED_NOTIFICATION_ID object:nil];

and notification is posted in some other place:

[FBRequestConnection startWithGraphPath:@"me/friends?fields=id,first_name,last_name,picture.type(small)" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

if (!error) {

[[NSNotificationCenter defaultCenter] postNotificationName:USERS_LOADED_NOTIFICATION_ID object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys: currentUser, @"user", friends, @"friends", nil]];

} else {

// An error occurred, we need to handle the error

// See: https://developers.facebook.com/docs/ios/errors

}

}];

addObserver method is called for both of mentioned classes, however notification is being delivered just to one observer. If I delete this observer(which receives the notification), then another one receives the notification.

Prior to iOS 8 both observers receive the notification.

Can you, please, help me with this issue?

网友答案:

Found answer. There is another way in iOS 8 to register for receiving remote notifications. I get nil for device token and app break on line:

NSDictionary *item = @{UID_ID : sCurrentUserId, @"deviceToken": appDelegate.deviceToken, @"handle": @"", @"friends": friends};

and second observer never receives notification.

网友答案:

You need to register your NSNotifications to work on iOS 8 and later but don't need to register if your iOS version is less than iOS 8. Use Following Code

 NSArray *vComp = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];

if ([[vComp objectAtIndex:0] intValue] >= 8) {
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationActivationModeBackground categories:nil]];

}

happy Coding

分享给朋友:
您可能感兴趣的文章:
随机阅读: