Añadir una función que llamaremos en el toque de un botón dentro del archivo YourViewController.h y luego darle al cuerpo a esa función en el YourViewController.m presentar
-(void)Trigger_LocalNotification
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *_localNotification = [[UILocalNotification alloc]init];
//setting the fire dat of the local notification
_localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
//setting the time zone
_localNotification.timeZone = [NSTimeZone defaultTimeZone];
//setting the message to display
_localNotification.alertBody = @"Did you forget something?";
//default notification sound
_localNotification.soundName = UILocalNotificationDefaultSoundName;
//displaying the badge number
_localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber]+1;
//schedule a notification at its specified time with the help of the app delegate
[[UIApplication sharedApplication]scheduleLocalNotification:_localNotification];
}
La primera línea del código elimina todas las notificaciones locales del sistema si se declaran. En la segunda línea, estoy inicializando la variable UILocalNotification y en la tercera línea estoy usando la propiedad fireDate para establecer la hora en que se activará esta notificación local y, como puede ver, la notificación se activará después de 5 segundos.
El soundName es una propiedad de la clase UILocalNotification que se utiliza para reproducir un sonido cuando se activa la notificación y cuando la aplicación que activa esta notificación local no está activa, en ese caso aparecerá un cuadro de alerta con la notificación predeterminada sonido y el mensaje de alerta está escrito usando la propiedad alertBody. La última línea del código adjuntará esta notificación al sistema.
asegúrese de conectar esta función con el botón táctil en el interior caso
[btn addTarget:self action:@selector(Trigger_LocalNotification) forControlEvents:UIControlEventTouchUpInside];
A continuación, seleccione el archivo de la aplicación Delegate.m de su proyecto y crear el objeto de esta clase (YourViewController)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
YourViewController *obj = [[YourViewController alloc]init];
[self.window addSubview:obj.view];
[self.window makeKeyAndVisible];
return YES;
}
Ejecute la aplicación y cuando la aplicación se inicie en el simulador, presione rápidamente el botón de inicio para ver el cuadro de alerta de la notificación local después de 5 seg.
Espero que esta respuesta te haya ayudado a aprender cómo implementar UILocalNotification.
gracias a que la alerta funciona bien ahora ..... Pero el texto para mostrarse en esa alerta es de otro controlador de vista y por qué no funciona ??. 'application.applicationIconBadgeNumber = 0; \t NSString * reminderText = [notification.userInfo objectForKey: kRemindMeNotificationDataKey]; \t [viewController showReminder: reminderText]; – Chandu
mostrar recordatorio es el método que escribí en el otro controlador de vista, pero no sé por qué no se llama – Chandu
buena manera de lidiar con notif local. ¿Sugiere algún lugar para establecer 'isAppResumingFromBackground' en' YES'? En algún lugar se llamará después de 'application: didReceiveLocalNotification:' pero se invocará en cualquier tipo de caso en que la aplicación se reanude. – dvkch