当应用程序退出10天时,iOS会创建本地通知

时间:2016-06-16 08:33:26

标签: ios localnotification

我在10天和15:30申请退出时创建本地通知。这是一个代码:

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *now = [NSDate date];
    float day = 10;
    NSDate *newDate = [now dateByAddingTimeInterval:60*60*24*day];

    NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit |  NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: newDate];

    [componentsForFireDate setHour: 15];
    [componentsForFireDate setMinute:30];
    [componentsForFireDate setSecond:0];
    NSDate *fireDateOfNotification = [calendar dateFromComponents:componentsForFireDate];
    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = fireDateOfNotification;
    localNotification.repeatInterval = NSWeekCalendarUnit;
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

当我尝试更改日期&时间在我的手机到第二天(现在+ 1),它仍然显示通知,第二天,仍然显示。我想在app退出时每10天发送一次本地通知。有人建议解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

尝试此代码:您的应用在10天通知火灾和其他1天火灾后打开。

 NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *componentsForFireDate = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear) fromDate: [NSDate date]];
componentsForFireDate.day = componentsForFireDate.day + 10;
[componentsForFireDate setHour: 15];
[componentsForFireDate setMinute:30];
[componentsForFireDate setSecond:0];
NSDate *fireDateOfNotification = [calendar dateFromComponents:componentsForFireDate];
NSLog(@"Date: %@",fireDateOfNotification);
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = fireDateOfNotification;
localNotification.alertBody = @"This is local notification!";
localNotification.repeatInterval = NSCalendarUnitDay;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

希望此代码可以帮助您。

答案 1 :(得分:0)

更改系统中的时间后,请同时检查模拟器时间。最初没有更新。

在模拟器设置中,General-> language&区域 - >高级 - > Automatica开关将处于开启状态。选择关闭。那么你的模拟器时间也会改变