iphone - 只让本地通知出现一次

标签 iphone objective-c ios notifications uilocalnotification

我有一个大问题!我的问题是,我已经在我的委托(delegate)中实现了本地通知代码。我已将此代码放入 didEnterBackround 方法中。但是,我为每个 didEnterBackround 获得了一个本地通知。如果最新的通知消失了,是否有可能只启动一个新的通知触发?就像我有一个 5 天后触发的 localnotifi 一样。当我打开和关闭应用程序 5 次并等待 5 天时,我会收到 5 个本地通知。是否有可能让应用程序只创建一个启动,然后出现一个新的启动?!

我的代码:

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

最佳答案

您可以取消所有预定的通知:

[[UIApplication sharedApplication] cancelAllLocalNotifications];

如果您不想删除所有已安排的通知(也许您还有其他安排),请查看 cancelLocalNotification:它只允许您取消一个实例。

关于iphone - 只让本地通知出现一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13204419/

相关文章:

iphone - 适用于 iOS 和 OS X 应用程序的库?

iphone - 不可编辑的 UITableView 部分?

ios - NSURLSessionUploadTask "finishes"但没有网络流量

objective-c - 使用 ARC 延迟释放包含的对象?

ios - hkworkout totalenergyburned 与 HKQuantityTypeIdentifierActiveEnergyBurned 不相关

ios - 如何在ios中的另一个类中获取NSDictionary JSON数据

iphone - MKMapView 在点击时隐藏导航栏,就像在照片应用程序中一样,而不会丢失 MKMapView 的功能

ios - 如果语句有时与 id BoolValue 比较失败?

ios - 如何在iOS的对象库中使用 "pdfView"?

android - PhoneGap 对比安卓和 iOS