我正在开发具有显示本地通知功能的应用程序。
在那里,我设置了特定日期的本地通知,之后我想连续显示84天(12周)。为此我使用NSDayCalendarUnit
重复类型。但这些提醒仍然在84天后显示出来。
我尝试使用- (void)applicationDidBecomeActive:(UIApplication *)application
方法取消通知,但在84天之后需要至少与app进行一次互动才能知道系统日期。
请提供一些解决方案或指南。以下代码我用来设置提醒。
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:cDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:cDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
// Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:0];
Class cls = NSClassFromString(@"UILocalNotification");
if (cls != nil) {
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = [calendar dateFromComponents:dateComps];;
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody =@"take your medicine";
notif.alertAction = @"View";
NSLog(@"SOUND=%d",[[NSUserDefaults standardUserDefaults] boolForKey:SWITCH_SOUND]);
if([[NSUserDefaults standardUserDefaults] boolForKey:SWITCH_SOUND]||cId>=100)
notif.soundName = UILocalNotificationDefaultSoundName;
notif.repeatInterval=NSCalendarUnitDay;
NSDictionary *userDict = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%d",cId]
forKey:NOTIFICATION_KEY];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
NSLog(@"Set reminder %d For Date =%@",cId,cDate);
}