2014-09-26 59 views
8

我将Xcode升级到Xcode 6.0.1,现在iOS8设备没有发生远程通知注册。它适用于iOS 7设备。为什么应用程序未在iOS 8中注册推送通知?

我已经加入在应用程序代理的代码如下所述:

//-- 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)]; 
} 

即使当前通知存在,并且它不是零。

然而下面的方法不叫:下面

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

截图解释说,我已经启用的后台模式的某些选项:

enter image description here

和通知在设备设置我的应用程序的设置。

回答

16

你需要调用

[[UIApplication sharedApplication] registerForRemoteNotifications]; 

在iOS8上的代码路径,注册用户通知设置后。

+2

非常感谢。现在,该应用正在成功注册推送通知。 – user1899840 2014-09-26 08:04:28

13

以下代码适用于iOS 8.0 Xcode 6.0或更高版本以及以下版本。

- (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

检查以下步骤希望它会帮助你

步骤1在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)]; 
    } 
} 

步骤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); 
}