2014-09-26 25 views
8

Tôi đã nâng cấp mã Xcode lên Xcode 6.0.1, hiện tại việc đăng ký thông báo từ xa không xảy ra đối với thiết bị iOS 8. Nó hoạt động tốt cho thiết bị iOS 7.Tại sao ứng dụng không được đăng ký cho các thông báo đẩy trong iOS 8?

Tôi đã thêm đoạn code trong ứng dụng delegate như đề cập dưới đây:

//-- Set Notification 
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) 
{ 
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge 
    |UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert) categories:nil]; 
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 

    NSLog(@"current notifications : %@", [[UIApplication sharedApplication] currentUserNotificationSettings]); 
} 
else 
{ 
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: 
    (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; 
} 

Ngay cả những thông báo hiện tại là hiện tại, và nó không phải là con số không.

Tuy nhiên phương pháp dưới đây không được gọi:

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken 

Ảnh chụp màn hình dưới đây giải thích rằng tôi đã kích hoạt tùy chọn nhất định trong chế độ background:

enter image description here

Và thông báo được thiết lập trong thiết bị cài đặt cho ứng dụng của tôi.

Trả lời

16

Bạn cần phải gọi

[[UIApplication sharedApplication] registerForRemoteNotifications]; 

trong đường dẫn mã iOS8 của bạn, sau khi đăng ký các thiết lập người dùng thông báo.

+2

Thanks a lot. Bây giờ ứng dụng đăng ký thành công các thông báo đẩy. – user1899840

13

Mã dưới đây sẽ hoạt động trong iOS 8.0 Xcode 6.0 trở lên và cũng cho các phiên bản bên dưới.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    //This code will work in iOS 8.0 xcode 6.0 or later 
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) 
    { 
     [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; 

     [[UIApplication sharedApplication] registerForRemoteNotifications]; 
    } 
    else 
    { 
     [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeNewsstandContentAvailability| UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; 
    } 

    return YES; 
} 
5

bước Kiểm tra sau hy vọng nó sẽ giúp bạn

bước 1 Trong didFinishLaunchingWithOptions

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) 
{ 
    //ios8 ++ 
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) 
    { 
     UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]; 
     [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings]; 
    } 
} 
else 
{ 
    // ios7 
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerForRemoteNotificationTypes:)]) 
    { 
     [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; 
    } 
} 

Bước 2

-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings // available in iOS8 
{ 
[application registerForRemoteNotifications]; 
} 
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
{ 
    NSString * token = [NSString stringWithFormat:@"%@", deviceToken]; 
//Format token as you need: 
token = [token stringByReplacingOccurrencesOfString:@" " withString:@""]; 
token = [token stringByReplacingOccurrencesOfString:@">" withString:@""]; 
token = [token stringByReplacingOccurrencesOfString:@"<" withString:@""]; 
NSLog(@"%@",token); 
} 
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{ 
// Handle your remote RemoteNotification 
} 

-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error 
{ 
NSLog(@"Error:%@",error); 
} 
Các vấn đề liên quan