Only let local notification appear once

早过忘川 提交于 2019-12-11 09:42:02

问题


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?!

My code:

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

回答1:


You're able to cancel all scheduled notifications:

[[UIApplication sharedApplication] cancelAllLocalNotifications];

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.



来源:https://stackoverflow.com/questions/13204419/only-let-local-notification-appear-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!