2012-09-12 187 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

相關問題