2011-06-29 36 views
5

He desactivado la notificación de inserción desde la aplicación de configuración de mi dispositivo (dentro del icono de mi aplicación en la configuración) y cuando llamo al siguiente fragmento de código no se llama a ninguna de las llamadas de mi delegado.Registrarse para notificación push

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound]; 

application:didRegisterForRemoteNotificationsWithDeviceToken: 
application:didFailToRegisterForRemoteNotificationsWithError: 

¿Hay alguna forma de saber antes de registrarse para Push qué todos los tipos de notificación se han activado? En mi aplicación, procedo más adelante una vez que recibo el token del dispositivo en la devolución de llamada didRegisterForRemoteNotificationsWithDeviceToken. Ahora, si el usuario no selecciona ninguno de ellos, no puedo avanzar más, así que también quiero dar una ruta alternativa.

Respuesta

10

Puede utilizar

UIRemoteNotificationType notificationTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; 

Y a continuación, comprobar la máscara de bits devuelta por lo que está y no está permitido

if (notificationTypes == UIRemoteNotificationTypeNone) { 
    // Do what ever you need to here when notifications are disabled 
} else if (notificationTypes == UIRemoteNotificationTypeBadge) { 
    // Badge only 
} else if (notificationTypes == UIRemoteNotificationTypeAlert) { 
    // Alert only 
} else if (notificationTypes == UIRemoteNotificationTypeSound) { 
    // Sound only 
} else if (notificationTypes == (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert)) { 
    // Badge & Alert 
} else if (notificationTypes == (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)) { 
    // Badge & Sound   
} else if (notificationTypes == (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)) { 
    // Alert & Sound 
} else if (notificationTypes == (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)) { 
    // Badge, Alert & Sound  
} 

Puede leer más en the docs here

+0

Gracias. Esto funciona pero no lee las banderas individuales por separado. Si solo hay una bandera habilitada (Sonido/Alerta/Placa), podemos leerla, pero si hay más de una marca habilitada, no obtenemos valor. Alguna pista de cómo manejar esto. Quiero leer todos estos indicadores de manera diferente. – Abhinav

+0

Como el valor devuelto es una máscara de bits, deberá probar manualmente cada combinación posible. He actualizado mi respuesta anterior para demostrar esto. –

+1

bdmontz answer es la forma correcta de comparar bit-masks. El ejemplo anterior es innecesariamente complejo. – Emil

9

me di cuenta que es poniéndose bastante viejo, pero hay una forma mucho mejor de verificar qué bits se configuran en una máscara de bits. En primer lugar, si solo desea comprobar si se ha configurado al menos un bit, compruebe si la máscara de bits completa no es cero.

if ([[UIApplication sharedApplication] enabledRemoteNotificationTypes] != 0) { 
    //at least one bit is set 
} 

Y si desea comprobar si hay bits específicos están estableciendo, de manera lógica y lo poco que desea comprobar con la máscara de bits.

UIRemoteNotificationType enabledTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; 
if (enabledTypes & UIRemoteNotificationTypeBadge) { 
    //UIRemoteNotificationTypeBadge is set in the bitmask 
} 
+0

Me da siempre UIRemoteNotificationTypeNone en iOS 7. ¿Alguna razón? –

Cuestiones relacionadas