0

我正在开发一个提醒到期日期的应用程序。我已经使用UILocalNotificationrepeat Interval实现了相同的功能(NSMonthCalendarUnit, NSDayCalendarUnit,NSDayCalendarUnit)。例如,我的开火日期是 01-01-2012,重复间隔是NSDayCalendarUnit,结束日期是 2012 年cancelLocalNotification:12 月 12 日,是否可以到期。

这是代码:-

- (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;
}
4

2 回答 2

1

简而言之,不,当您的应用程序在后台运行时,您无法取消 UILocalNotification。

于 2012-09-12T13:34:14.840 回答
0
                 **Apple RESPONSE:-**

我正在回答您关于 UILocalNotification 的问题。

目前 UILocalNotification 没有办法在通知自动取消之前指定到期日期或重复次数。

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

但你是对的;如果用户从未将您的应用程序带到前台,它就没有机会取消或重新安排本地通知。

我强烈建议您在 < https://developer.apple.com/bugreporter > 提交增强请求,要求在未来的 iOS 版本中提供此功能。

您还询问了当用户从设备中删除您的应用程序时本地通知会发生什么情况。iOS 存储本地通知,以便在用户删除然后重新安装应用程序时仍会安排它们。

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

最好的问候,--gc


Garth Cummings Apple 开发者技术支持

于 2012-09-18T14:19:28.623 回答