我就提醒有關到期日一個應用程序的工作。我已使用UILocalNotification
與重複間隔(NSMonthCalendarUnit, NSDayCalendarUnit,NSDayCalendarUnit)
實施相同。例如,我對2012年1月1日火日期和重複間隔時間爲NSDayCalendarUnit
和結束日期爲12-12-2012,是possbile到cancelLocalNotification:
期滿。IOS CancelNotificaion當應用程序在後臺
這裏是代碼: -
- (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;
}
我已經安排UILocalNotification重複間隔NSDaycalendar。通知應每天重複90天,第91天應該取消。如何取消通知當應用程序運行不運行時是Foreground?是否有任何特定的屬性,我們可以設置UILocalnotification的最後日期。 – Hariharan
我明白你想要做什麼,但這是不可能的。沒有可以設置的屬性。我研究了一些類似於你想要做的一些事情,並找不到解決方案。如果你能找到解決方案,我很樂意聽到它。 – Darren
我找不到任何解決方案。我所做的是,如果通知發送到應用程序時,我檢查它是否仍然有效,那麼如果應該過期,我將其取消並向用戶顯示一條消息,告知此通知已過期。 – IgniteCoders