只让本地通知出现一次 [英] Only let local notification appear once

查看:39
本文介绍了只让本地通知出现一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大问题!我的问题是,我已经在委托中实现了一个本地通知代码.我已将此代码放入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.

这篇关于只让本地通知出现一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆