unresolved notification objective-c iphone uilocalnotification usernotifications

objective-c - unresolved - unusernotificationcenter



Eliminar una notificación local particular (12)

Estoy desarrollando una aplicación de alarma para iPhone basada en notificaciones locales.

Al eliminar una alarma, la notificación local relacionada debe ser cancelada. Pero, ¿cómo puedo determinar exactamente qué objeto de la matriz de notificaciones locales se va a cancelar?

Estoy al tanto del [[UIApplication sharedApplication] cancelLocalNotification:notification] pero ¿cómo puedo obtener esta ''notificación'' para cancelarlo?


El objeto UILocalNotification que pasa a cancelLocalNotification: coincidirá con cualquier objeto UILocalNotification existente con propiedades coincidentes.

Asi que:

UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.alertBody = @"foo"; [[UIApplication sharedApplication] presentLocalNotificationNow:notification];

presentará una notificación local que luego se puede cancelar con:

UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.alertBody = @"foo"; [[UIApplication sharedApplication] cancelLocalNotification:notification];


Esto es lo que hago.

Al crear su notificación, haga esto:

// Create the notification UILocalNotification *notification = [[UILocalNotification alloc] init] ; notification.fireDate = alertDate; notification.timeZone = [NSTimeZone localTimeZone] ; notification.alertAction = NSLocalizedString(@"Start", @"Start"); notification.alertBody = **notificationTitle**; notification.repeatInterval= NSMinuteCalendarUnit; notification.soundName=UILocalNotificationDefaultSoundName; notification.applicationIconBadgeNumber = 1; [[UIApplication sharedApplication] scheduleLocalNotification:notification] ;

cuando intentas eliminarlo haz esto:

NSArray *arrayOfLocalNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications] ; for (UILocalNotification *localNotification in arrayOfLocalNotifications) { if ([localNotification.alertBody isEqualToString:savedTitle]) { NSLog(@"the notification this is canceld is %@", localNotification.alertBody); [[UIApplication sharedApplication] cancelLocalNotification:localNotification] ; // delete the notification from the system } }

Esta solución debería funcionar para múltiples notificaciones, y usted no administra matrices o diccionarios o valores predeterminados del usuario. Simplemente usa los datos que ya ha guardado en la base de datos de notificaciones del sistema.

Espero que esto ayude a los futuros diseñadores y desarrolladores.

Feliz codificación chicos! :RE


Expandí un poco la respuesta de KingofBliss, escribí esto un poco más parecido a Swift2, eliminé un código innecesario y agregué algunos protectores contra choques.

Para comenzar, al crear la notificación, debe asegurarse de configurar el uid (o cualquier propiedad personalizada realmente) del userInfo de la notificación:

notification.userInfo = ["uid": uniqueid]

Luego, al eliminarlo, puede hacer:

guard let app: UIApplication = UIApplication.sharedApplication(), let notifications = app.scheduledLocalNotifications else { return } for notification in notifications { if let userInfo = notification.userInfo, let uid: String = userInfo["uid"] as? String where uid == uidtodelete { app.cancelLocalNotification(notification) print("Deleted local notification for ''/(uidtodelete)''") } }


La solution de iMOBDEV funciona perfectamente para eliminar una notificación específica (por ejemplo, después de eliminar la alarma), pero es especialmente útil cuando debe eliminar selectivamente cualquier notificación que ya haya sido activada y todavía se encuentre en el centro de notificaciones.

Un posible escenario sería: la notificación de una alarma se dispara, pero el usuario abre la aplicación sin tocar esa notificación y programa esa alarma nuevamente. Si desea asegurarse de que solo una notificación puede estar en el centro de notificación para un elemento / alarma determinado, es un buen enfoque. También le permite no tener que borrar todas las notificaciones cada vez que se abre la aplicación, debe ajustarse mejor a la aplicación.

  • Al crear una notificación local, use NSKeyedArchiver para almacenarla como Data en UserDefaults . Puede crear una clave igual a la que está guardando en el diccionario userInfo de la notificación. Si está asociado con un objeto Core Data, podría usar su propiedad unique objectID.
  • Recuperarlo con NSKeyedUnarchiver . Ahora puede eliminarlo utilizando el método cancelLocalNotification.
  • Actualice la clave en UserDefaults consecuencia.

Aquí hay una versión de Swift 3.1 de esa solución (para destinos debajo de iOS 10):

Almacenar

// localNotification is the UILocalNotification you''ve just set up UIApplication.shared.scheduleLocalNotification(localNotification) let notificationData = NSKeyedArchiver.archivedData(withRootObject: localNotification) UserDefaults.standard.set(notificationData, forKey: "someKeyChosenByYou")

Recuperar y eliminar

let userDefaults = UserDefaults.standard if let existingNotificationData = userDefaults.object(forKey: "someKeyChosenByYou") as? Data, let existingNotification = NSKeyedUnarchiver.unarchiveObject(with: existingNotificationData) as? UILocalNotification { // Cancel notification if scheduled, delete it from notification center if already delivered UIApplication.shared.cancelLocalNotification(existingNotification) // Clean up userDefaults.removeObject(forKey: "someKeyChosenByYou") }


Otra opción:

En primer lugar, cuando crea una notificación local, puede almacenarla en los valores predeterminados del usuario para usarla en el futuro. El objeto de notificación local no se puede almacenar directamente en los valores predeterminados del usuario. Este objeto primero se debe convertir en NSData y luego se puede almacenar NSData en los User defaults . Debajo hay un código para eso:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:localNotif]; [[NSUserDefaults standardUserDefaults] setObject:data forKey:[NSString stringWithFormat:@"%d",indexPath.row]];

Después de haber almacenado y programado la notificación local, en el futuro, puede surgir el requisito de que deba cancelar cualquiera de las notificaciones que creó anteriormente, para que pueda recuperarla de los valores predeterminados del Usuario.

NSData *data= [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%d",UniqueKey]]; UILocalNotification *localNotif = [NSKeyedUnarchiver unarchiveObjectWithData:data]; NSLog(@"Remove localnotification are %@", localNotif); [[UIApplication sharedApplication] cancelLocalNotification:localNotif]; [[NSUserDefaults standardUserDefaults] removeObjectForKey:[NSString stringWithFormat:@"%d",UniqueKey]];

Espero que esto ayude


Para recordatorios repetidos (por ejemplo, desea que su alarma se encienda el domingo, el sábado y el miércoles a las 4 p. M., Luego debe hacer 3 alarmas y configurar repeatInterval en NSWeekCalendarUnit).

Para hacer Recordatorio de Once Only:

UILocalNotification *aNotification = [[UILocalNotification alloc] init]; aNotification.timeZone = [NSTimeZone defaultTimeZone]; aNotification.alertBody = _reminderTitle.text; aNotification.alertAction = @"Show me!"; aNotification.soundName = UILocalNotificationDefaultSoundName; aNotification.applicationIconBadgeNumber += 1; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit| NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: _reminderDate]; [componentsForFireDate setHour: [componentsForFireDate hour]] ; //for fixing 8PM hour [componentsForFireDate setMinute:[componentsForFireDate minute]]; [componentsForFireDate setSecond:0] ; NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate]; aNotification.fireDate = fireDateOfNotification; NSDictionary *infoDict = [NSDictionary dictionaryWithObject:_reminderTitle.text forKey:kRemindMeNotificationDataKey]; aNotification.userInfo = infoDict; [[UIApplication sharedApplication] scheduleLocalNotification:aNotification];

Para hacer un recordatorio repetido:

for (int i = 0 ; i <reminderDaysArr.count; i++) { UILocalNotification *aNotification = [[UILocalNotification alloc] init]; aNotification.timeZone = [NSTimeZone defaultTimeZone]; aNotification.alertBody = _reminderTitle.text; aNotification.alertAction = @"Show me!"; aNotification.soundName = UILocalNotificationDefaultSoundName; aNotification.applicationIconBadgeNumber += 1; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit| NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: _reminderDate]; [componentsForFireDate setWeekday: [[reminderDaysArr objectAtIndex:i]integerValue]]; [componentsForFireDate setHour: [componentsForFireDate hour]] ; // Setup Your Own Time. [componentsForFireDate setMinute:[componentsForFireDate minute]]; [componentsForFireDate setSecond:0] ; NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate]; aNotification.fireDate = fireDateOfNotification; aNotification.repeatInterval = NSWeekCalendarUnit; NSDictionary *infoDict = [NSDictionary dictionaryWithObject:_reminderTitle.text forKey:kRemindMeNotificationDataKey]; aNotification.userInfo = infoDict; [[UIApplication sharedApplication] scheduleLocalNotification:aNotification]; } }

Para filtrar tu matriz para mostrarla.

-(void)filterNotficationsArray:(NSMutableArray*) notificationArray{ _dataArray = [[NSMutableArray alloc]initWithArray:[[UIApplication sharedApplication] scheduledLocalNotifications]]; NSMutableArray *uniqueArray = [NSMutableArray array]; NSMutableSet *names = [NSMutableSet set]; for (int i = 0 ; i<_dataArray.count; i++) { UILocalNotification *localNotification = [_dataArray objectAtIndex:i]; NSString * infoDict = [localNotification.userInfo objectForKey:@"kRemindMeNotificationDataKey"]; if (![names containsObject:infoDict]) { [uniqueArray addObject:localNotification]; [names addObject:infoDict]; } } _dataArray = uniqueArray; }

Para eliminar Recordatorio, incluso si fue Solo una vez o Repetido:

- (void) removereminder:(UILocalNotification*)notification { _dataArray = [[NSMutableArray alloc]initWithArray:[[UIApplication sharedApplication]scheduledLocalNotifications]]; NSString * idToDelete = [notification.userInfo objectForKey:@"kRemindMeNotificationDataKey"]; for (int i = 0 ; i<_dataArray.count; i++) { UILocalNotification *currentLocalNotification = [_dataArray objectAtIndex:i]; NSString * notificationId = [currentLocalNotification.userInfo objectForKey:@"kRemindMeNotificationDataKey"]; if ([notificationId isEqualToString:idToDelete]) [[UIApplication sharedApplication]cancelLocalNotification:currentLocalNotification]; } _dataArray = [[NSMutableArray alloc]initWithArray:[[UIApplication sharedApplication]scheduledLocalNotifications]]; [self filterNotficationsArray:_dataArray]; [_remindersTV reloadData]; }


Programar y eliminar la notificación en swift:

static func scheduleNotification(notificationTitle:String, objectId:String) { var localNotification = UILocalNotification() localNotification.fireDate = NSDate(timeIntervalSinceNow: 24*60*60) localNotification.alertBody = notificationTitle localNotification.timeZone = NSTimeZone.defaultTimeZone() localNotification.applicationIconBadgeNumber = 1 //play a sound localNotification.soundName = UILocalNotificationDefaultSoundName; localNotification.alertAction = "View" var infoDict : Dictionary<String,String!> = ["objectId" : objectId] localNotification.userInfo = infoDict; UIApplication.sharedApplication().scheduleLocalNotification(localNotification) } static func removeNotification(objectId:String) { var app:UIApplication = UIApplication.sharedApplication() for event in app.scheduledLocalNotifications { var notification = event as! UILocalNotification var userInfo:Dictionary<String,String!> = notification.userInfo as! Dictionary<String,String!> var infoDict : Dictionary = notification.userInfo as! Dictionary<String,String!> var notifcationObjectId : String = infoDict["objectId"]! if notifcationObjectId == objectId { app.cancelLocalNotification(notification) } } }


Puede guardar un valor único para la clave en la información de usuario de su notificación local. Obtenga toda la notificación local, recorra el conjunto y elimine la notificación en particular.

Código de la siguiente manera,

OBJ-C:

UIApplication *app = [UIApplication sharedApplication]; NSArray *eventArray = [app scheduledLocalNotifications]; for (int i=0; i<[eventArray count]; i++) { UILocalNotification* oneEvent = [eventArray objectAtIndex:i]; NSDictionary *userInfoCurrent = oneEvent.userInfo; NSString *uid=[NSString stringWithFormat:@"%@",[userInfoCurrent valueForKey:@"uid"]]; if ([uid isEqualToString:uidtodelete]) { //Cancelling local notification [app cancelLocalNotification:oneEvent]; break; } }

RÁPIDO:

var app:UIApplication = UIApplication.sharedApplication() for oneEvent in app.scheduledLocalNotifications { var notification = oneEvent as UILocalNotification let userInfoCurrent = notification.userInfo! as [String:AnyObject] let uid = userInfoCurrent["uid"]! as String if uid == uidtodelete { //Cancelling local notification app.cancelLocalNotification(notification) break; } }

UserNotification:

Si usa UserNotification (iOS 10+), simplemente siga estos pasos:

  1. Al crear el contenido UserNotification, agregue un identifier único

  2. Elimina notificaciones pendientes específicas usando removePendingNotificationRequests(withIdentifiers:)

  3. Eliminar notificación entregada específica utilizando removeDeliveredNotifications(withIdentifiers:)

Para más información, UNUserNotificationCenter


Puede mantener una cadena con el identificador de categoría al programar la notificación como tal

localNotification.category = NotificationHelper.categoryIdentifier

y buscar y cancelar cuando sea necesario como tal

let app = UIApplication.sharedApplication() for notification in app.scheduledLocalNotifications! { if let cat = notification.category{ if cat==NotificationHelper.categoryIdentifier { app.cancelLocalNotification(notification) break } } }


Versión Swift, si es necesario:

func cancelLocalNotification(UNIQUE_ID: String){ var notifyCancel = UILocalNotification() var notifyArray = UIApplication.sharedApplication().scheduledLocalNotifications for notifyCancel in notifyArray as! [UILocalNotification]{ let info: [String: String] = notifyCancel.userInfo as! [String: String] if info[uniqueId] == uniqueId{ UIApplication.sharedApplication().cancelLocalNotification(notifyCancel) }else{ println("No Local Notification Found!") } } }


Yo uso esta función en Swift 2.0:

static func DeleteNotificationByUUID(uidToDelete: String) -> Bool { let app:UIApplication = UIApplication.sharedApplication() // loop on all the current schedualed notifications for schedualedNotif in app.scheduledLocalNotifications! { let notification = schedualedNotif as UILocalNotification let urrentUi = notification.userInfo! as! [String:AnyObject] let currentUid = urrentUi["uid"]! as! String if currentUid == uidToDelete { app.cancelLocalNotification(notification) return true } } return false }

Inspirado en la respuesta de @ KingofBliss


veloz estilo 3:

final private func cancelLocalNotificationsIfIOS9(){ //UIApplication.shared.cancelAllLocalNotifications() let app = UIApplication.shared guard let notifs = app.scheduledLocalNotifications else{ return } for oneEvent in notifs { let notification = oneEvent as UILocalNotification if let userInfoCurrent = notification.userInfo as? [String:AnyObject], let uid = userInfoCurrent["uid"] as? String{ if uid == uidtodelete { //Cancelling local notification app.cancelLocalNotification(notification) break; } } }

}

para el uso de iOS 10:

let center = UNUserNotificationCenter.current() center.removePendingNotificationRequests(withIdentifiers: [uidtodelete])