2011-07-28 12 views

Respuesta

8

No, recibirá una notificación en el delegado de la aplicación.

- (void) application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification { 
    //Place your code to handle the notification here. 
} 
+0

muchas gracias. Ahora, mantuve un UIAlertView en la aplicación método de notificación de notificación incorrecta para que pueda usarlo en lugar de la notificación cuando la aplicación ya se está ejecutando. Pero cuando la aplicación está en segundo plano y la notificación se activa y cuando la aplicación entra en primer plano, se llama a este método y aparece alertView. ¿Puedes decirme cómo puedo evitar esto? –

+0

Bien, tenemos applicationWillEnterForeground: método para eso. Lo siento estúpida pregunta! Muchas gracias. –

+0

No es una pregunta estúpida: me acabas de dar la respuesta que estaba buscando :-) –

0

si su aplicación se encuentra actualmente en primer plano la siguiente función se llamará en su Delegado:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)Notifikation 

A continuación, puede elegir si desea mostrar un alertview, pero la norma no se mostrará por sí mismo

+1

Esto se llama independientemente de si la aplicación ya se estaba ejecutando o no. –

+0

Solo si el usuario hace clic en el botón "abrir" ... – Bastian

3

Hice una lib para hacer una animación casi igual que las notificaciones locales.

Comprobar esto: https://github.com/OpenFibers/OTNotification

Demostración: enter image description here

enter image description here

Y puede publicar un nuevo mensaje a este lib cuando recibió un mensaje en

- (void) application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification 
{ 
    OTNotificationManager *notificationManager = [OTNotificationManager defaultManager]; 
    OTNotificationMessage *notificationMessage = [[OTNotificationMessage alloc] init]; 
    notificationMessage.title = [self notificationTitle]; 
    notificationMessage.message = @"A notification. Touch me to hide me."; 
    [notificationManager postNotificationMessage:notificationMessage]; 
} 
2

El aceptado anser es correcto, pero no es suficiente t o reciba todas las notificaciones y muestre algo al usuario desde

- (void) application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification { 

Debe verificar, esta es la notificación actual o no. A veces hay otros avisos (cuando los cancelas, por ejemplo). Por lo tanto, usted tiene que comprobar, que es lo que, excepto:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { 
    if (fabs([[NSDate date] timeIntervalSinceDate:[notification fireDate]]) <= 0.5f) 
    { 
     [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Notification alert", @"") 
            message:notification.alertBody 
            delegate:self 
          cancelButtonTitle:@"Ok" otherButtonTitles:nil] show];  
    } 
} 
0

Swift 2.2:

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) { 
    var state = application.applicationState 
    if state == .Active { 
     // handle the notification, e.g. show an alert 
    } 
} 

Swift 3.0:

func application(_ application: UIApplication, didReceive notification: UILocalNotification) { 
    var state: UIApplicationState = application.applicationState 
    if state == .active { 
     // handle the notification, e.g. show an alert 
    } 
} 
Cuestiones relacionadas