2012-09-12 151 views
0

我就提醒有关到期日一个应用程序的工作。我已使用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; 
} 

回答

1

总之,不,你不能取消UILocalNotification,而您的应用程序在后台运行。

+0

我已经安排UILocalNotification重复间隔NSDaycalendar。通知应每天重复90天,第91天应该取消。如何取消通知当应用程序运行不运行时是Foreground?是否有任何特定的属性,我们可以设置UILocalnotification的最后日期。 – Hariharan

+0

我明白你想要做什么,但这是不可能的。没有可以设置的属性。我研究了一些类似于你想要做的一些事情,并找不到解决方案。如果你能找到解决方案,我很乐意听到它。 – Darren

+0

我找不到任何解决方案。我所做的是,如果通知发送到应用程序时,我检查它是否仍然有效,那么如果应该过期,我将其取消并向用户显示一条消息,告知此通知已过期。 – IgniteCoders

0
    **Apple RESPONSE:-** 

我回应了你对UILocalNotification的问题。

此时UILocalNotification无法指定到期日期或重复次数 ,然后自动取消通知 。

你今天可以得到最接近的是安排多达64个独立 通知,而不是使用重复间隔。

但你是正确的;如果用户没有永远把你的应用的前景 ,它不会有机会取消或重新安排当地 通知。

我强烈建议您在< https://developer.apple.com/bugreporter文件的增强请求>请求中的iOS 将来的版本中此功能。

你还问什么时候用户 将删除设备您的应用程序发生了什么地方通知。如果用户删除并重新安装 应用程序,则iOS将存储本地通知,以便它们仍在计划中。

当您的应用运行时,它可以检查UIApplication的scheduledLocalNotifications属性 并删除任何不再相关的通知。

最好的问候, --gc


加思·卡明斯 苹果开发者技术支持

+0

也请在应用程序还应该能够在通知时决定是否让它显示或抑制通知的“增强报告”中......似乎很明显,但显然不够明显.. – hokkuk

+0

通过能够在后台运行10秒来执行逻辑来决定通知是否应该通过.. ontop,为了能够注册iOS通知,所以应用程序可以做出决定发布通知因为一个应用程序可以被设计为与...一起工作的事件...例如通知粘贴板更改...(并且能够通过在10secs的背景中运行的快速例程来决定是否需要成为通知给用户) – hokkuk

相关问题