我正在处理一个提醒有效期的申请。我使用重复间隔UILocalNotification
使用(NSMonthCalendarUnit, NSDayCalendarUnit,NSDayCalendarUnit)
实现了相同的功能。例如,我的发布日期是01-01-2012,重复间隔是NSDayCalendarUnit
,结束日期是12-12-2012,到期时cancelLocalNotification:
是否可能。
这是代码: -
- (void) scheduleNotificationOn:(NSDate*) fireDate
text:(NSString*) alertText
action:(NSString*) alertAction
sound:(NSString*) soundfileName
launchImage:(NSString*) launchImage
andInfo:(NSDictionary*) userInfo
{
userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
txtExpiryDate.text, @"ExpiryDate",
txtRegNo.text , @"RegNo",
nil];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = fireDate;
localNotification.timeZone = [NSTimeZone systemTimeZone];
localNotification.userInfo = userInfo;
localNotification.alertBody = alertText;
localNotification.alertAction = alertAction;
NSLog(@"Repeat Type:%@",txtRepeat.text);
if([txtRepeat.text isEqualToString:@"Every Week"])
{
NSLog(@"Every Week");
localNotification.repeatInterval = 256;
}
else if([txtRepeat.text isEqualToString:@"Every Month"])
{
NSLog(@"Every Month");
localNotification.repeatInterval = NSMonthCalendarUnit;
}
else if([txtRepeat.text isEqualToString:@"Every Day"])
{
NSLog(@"Every Day");
localNotification.repeatInterval = NSDayCalendarUnit;
}
if(soundfileName == nil)
{
localNotification.soundName = UILocalNotificationDefaultSoundName;
}
else
{
localNotification.soundName = soundfileName;
}
NSLog(@"appDelegate.BadgeNumber:%d",appDelegate.BadgeNumber);
localNotification.alertLaunchImage = launchImage;
appDelegate.BadgeNumber = appDelegate.BadgeNumber + 1;
localNotification.applicationIconBadgeNumber = appDelegate.BadgeNumber;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
}
我的工作是将当前日期与到期日期进行比较。但是这项工作只有在应用程序处于前台并且我无法取消通知而不是特定日期的背景时。请找到相同的以下代码: -
- (void)applicationDidBecomeActive:(UIApplication *)application
{
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
BadgeNumber = 0;
application.applicationIconBadgeNumber = BadgeNumber;
NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSDateFormatter *formatter =[[[NSDateFormatter alloc]init] autorelease];
[formatter setDateFormat:@"dd/MM/yyyy"];
NSLog(@"localNotifications Count %d",localNotifications.count);
for (UILocalNotification *notify in localNotifications)
{
//notify.applicationIconBadgeNumber = 0;
NSString *ExpiryDateString = [notify.userInfo objectForKey:@"ExpiryDate"];
NSDate *ExpiryDate = [formatter dateFromString:ExpiryDateString];
NSDate * NextFireDate = nil;
NSLog(@"Expiry Date:%@",ExpiryDateString);
if(notify.repeatInterval == NSDayCalendarUnit)
{
NSLog(@"Repeat Every Day");
NextFireDate = [[NSDate date] dateByAddingDays:1];
NSLog(@"Next FireDate: %@",[formatter stringFromDate:NextFireDate]);
}
if(notify.repeatInterval == NSWeekCalendarUnit)
{
NSLog(@"Repeat Every Day");
NextFireDate = [[NSDate date] addTimeInterval:D_WEEK];
NSLog(@"Next FireDate: %@",[formatter stringFromDate:NextFireDate]);
}
if(notify.repeatInterval == NSMonthCalendarUnit)
{
NSLog(@"Repeat Every Day");
//NextFireDate = [[NSDate date] addTimeInterval:D_Month];
NextFireDate = [self CalculateExipiryDateForMonth];
NSLog(@"Next FireDate: %@",[formatter stringFromDate:NextFireDate]);
}
NSComparisonResult result = [NextFireDate compare:ExpiryDate];
NSLog(@"NSComparisonResult:%d",result);
if(result == NSOrderedDescending)
{
NSLog(@"Cancell......... Notification");
NSLog(@"notify :::%@",notify);
}
else
{
NSLog(@"Re-Schedule Notification");
BadgeNumber = BadgeNumber + 1;
notify.applicationIconBadgeNumber = BadgeNumber;
NSLog(@"BadgeNumber:%d",BadgeNumber);
[[UIApplication sharedApplication] scheduleLocalNotification:notify];
}
}
}
-(NSDate*) CalculateExipiryDateForMonth
{
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.month = 1;
NSDate *nextMonth = [gregorian dateByAddingComponents:components toDate:[NSDate date] options:0];
[components release];
NSDateComponents *nextMonthComponents = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:nextMonth];
NSDate *expiryDay = [gregorian dateFromComponents:nextMonthComponents];
NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = -1;
NSDate *NewExpiry = [gregorian dateByAddingComponents:dayComponent toDate:expiryDay options:0];
[gregorian release];
[dayComponent release];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"dd/MM/yyyy"];
NSLog(@"Next Exipiry Date -----:%@",[formatter stringFromDate:NewExpiry]);
return NewExpiry;
}
答案 0 :(得分:1)
简而言之,不,当您的应用在后台运行时,您无法取消UILocalNotification。
答案 1 :(得分:0)
**Apple RESPONSE:-**
我正在回答您关于UILocalNotification的问题。
此时UILocalNotification无法指定到期日期 通知自动之前的重复日期或次数 取消。
今天最接近的是安排最多64个人 通知而不是使用重复间隔。
但你是对的;如果用户没有将您的应用程序带到 前台,它将没有机会取消或重新安排本地 通知。
我强烈建议你在< https://developer.apple.com/bugreporter>在a中要求这个功能 iOS的未来版本。
您还询问了用户在本地通知时会发生什么 从设备中删除您的应用。 iOS存储本地通知 如果用户删除然后重新安装,它们仍然被安排 应用
当您的应用运行时,它可以检查scheduledLocalNotifications属性 UIApplication并删除任何不再相关的通知。
祝你好运, --gc