只让本地通知出现一次

编程入门 行业动态 更新时间:2024-10-25 18:26:19
本文介绍了只让本地通知出现一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个大问题!我的问题是,我已经在委托中实现了一个本地通知代码.我已将此代码放入didEnterBackround方法中.但是,对于每个didEnterBackround,我都会得到一个localnotifi.如果最近的通知失效,是否有可能仅启动新的通知触发日期?就像我有一个会在5天后触发的localnotifi一样.当我打开和关闭应用程序5次并等待5天时,我会收到5条本地通知.可以让该应用程序仅触发一次触发,然后在该触发之后出现一个新触发吗?!

I have a huge problem! My problem is, that I've implementet a localnotification code in my delegate. I've put in this code in didEnterBackround method. However, I get a localnotifi for each didEnterBackround. Is there a possible way that only a new notification firedate starts, if the latest was gone? Like if I have a localnotifi which fires after 5 days. When I open and close the app 5 times and wait 5 days I get 5 localnotifications. Is it possible to let the app make only one firedate and after this appears a new one gets startet?!

我的代码:

NSCalendar *calender = [NSCalendar autoupdatingCurrentCalendar]; NSDate *currentDate = [NSDate date]; NSDateComponents *dateComponents = [calender components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:currentDate]; NSDateComponents *timeComponents = [calender components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:currentDate]; NSDateComponents *temp = [[NSDateComponents alloc]init]; [temp setYear:[dateComponents year]]; [temp setMonth:[dateComponents month]]; [temp setDay:[dateComponents day]]; [temp setHour:[timeComponents hour]]; [temp setMinute:[timeComponents minute]]; [temp setSecond:[timeComponents second] +10]; NSDate *fireTime = [calender dateFromComponents:temp]; UILocalNotification *localN = [[UILocalNotification alloc]init]; localN.fireDate = fireTime; localN.timeZone = [NSTimeZone defaultTimeZone]; localN.alertBody = @"BLA BLA BLA!"; localN.alertAction = @"GO BACK"; localN.applicationIconBadgeNumber = 1; [[UIApplication sharedApplication] scheduleLocalNotification:localN];

推荐答案

您可以取消所有计划的通知:

You're able to cancel all scheduled notifications:

[[UIApplication sharedApplication] cancelAllLocalNotifications];

如果您不想删除所有计划的通知(也许您还有其他计划的通知),请查看 cancelLocalNotification:,它仅允许您取消单个实例.

If you don't want to remove ALL scheduled notifications (perhaps you have something else scheduled), then take a look at cancelLocalNotification: which allows you to cancel only a single instance.

更多推荐

只让本地通知出现一次

本文发布于:2023-11-27 07:02:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637067.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通知

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!