2014-09-26 7 views
8

Ho aggiornato il mio Xcode a Xcode 6.0.1, ora la registrazione delle notifiche remote non sta accadendo per il dispositivo iOS 8. Funziona perfettamente per il dispositivo iOS 7.Perché l'app non viene registrata per le notifiche push in iOS 8?

Ho aggiunto il codice in App delegato come indicato di seguito:

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

Anche la notifica corrente è presente, e non è pari a zero.

Eppure il metodo seguito non si chiama:

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

screenshot qui sotto spiega che ho permesso a determinate opzioni in modalità background:

enter image description here

E la notifica è impostata nel dispositivo impostazioni per la mia app.

risposta

16

è necessario chiamare

[[UIApplication sharedApplication] registerForRemoteNotifications]; 

nel percorso del codice iOS 8, dopo aver registrato le impostazioni di notifica all'utente.

+2

Grazie mille. Ora l'app si sta registrando con successo per le notifiche push. – user1899840

13

Il codice riportato di seguito funziona con iOS 8.0 Xcode 6.0 o versioni successive e anche per le versioni successive.

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

Controlla i seguenti passaggi spero che vi aiuterà a

passaggi 1 In 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)]; 
    } 
} 

Fase 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); 
}