objective c pantalla ¿Por qué la aplicación no se está registrando para notificaciones push en iOS 8?



notificaciones whatsapp iphone pantalla bloqueada (3)

Actualicé mi Xcode a Xcode 6.0.1, ahora el registro de notificación remota no está ocurriendo para el dispositivo con iOS 8. Está funcionando bien para el dispositivo iOS 7.

He agregado el código en el delegado de la aplicación como se menciona a continuación:

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

Incluso la notificación actual está presente, y no es nula.

Y sin embargo, el siguiente método no se llama:

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

La siguiente captura de pantalla explica que he habilitado ciertas opciones en el modo de fondo:

Y la notificación se establece en la configuración del dispositivo para mi aplicación.


Answer #1

Comprueba los siguientes pasos, espero que te ayude

Pasos 1 en 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)];
    }
}

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

Answer #2

Tienes que llamar

[[UIApplication sharedApplication] registerForRemoteNotifications];

en su ruta de código iOS8, después de registrar la configuración de notificación de usuario.


Answer #3

El código siguiente funcionará en iOS 8.0 Xcode 6.0 o posterior y también para versiones inferiores.

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




xcode6