为了账号安全,请及时绑定邮箱和手机立即绑定

删除特定的本地通知

删除特定的本地通知

墨色风雨 2019-07-30 11:07:56
删除特定的本地通知我正在开发基于本地通知的iPhone闹钟应用程序。删除警报时,相关的本地通知应取消。但是,如何确定要取消本地通知数组中的哪个对象呢?我知道[[UIApplication sharedApplication] cancelLocalNotification:notification]方法,但我怎么能得到这个'通知'取消它?
查看完整描述

3 回答

?
慕仙森

TA贡献1827条经验 获得超7个赞

其他选择:

首先,当您创建本地通知时,可以将其存储在用户默认值中以供将来使用,本地通知对象不能直接存储在用户默认值中,此对象需要先转换为NSData对象,然后NSData才能存入User defaults。以下是代码:

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

存储和计划本地通知后,将来可能需要取消您之前创建的任何通知,因此您可以从用户默认值中检索它。

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]];

希望这可以帮助


查看完整回答
反对 回复 2019-07-30
?
茅侃侃

TA贡献1842条经验 获得超21个赞

这就是我做的。

创建通知时,请执行以下操作:

  // Create the notificationUILocalNotification *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] ;

当试图删除它时,请执行以下操作:

 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

    }}

此解决方案应适用于多个通知,并且您不管理任何阵列或字典或用户默认值。您只需使用已保存到系统通知数据库的数据即可。

希望这有助于未来的设计师和开发者


查看完整回答
反对 回复 2019-07-30
  • 3 回答
  • 0 关注
  • 635 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信