我安排了一个通知,并在它显示警报消息之前给它 60 分钟的警告......
一旦我添加通知,我的 App Delegate 中的方法就会被调用:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
我怎么知道它会在后台显示为警报?是否有任何其他委托方法我需要覆盖或使用以确保给定具有 60 分钟间隔的预定警报...
- (void)scheduleNotificationWithItem:(NSDate *)item interval:(int)minutesBefore
{
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
//NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *currentDateComponents = [calendar components:( NSWeekdayCalendarUnit |
NSYearCalendarUnit | NSMonthCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSWeekCalendarUnit | NSMinuteCalendarUnit) fromDate:item];
NSLog(@"- current components year = %i , month = %i , week = % i, weekday = %i", [currentDateComponents year], [currentDateComponents month], [currentDateComponents week], [currentDateComponents weekday]);
NSLog(@"[currentDateComponents minute]: %i", [currentDateComponents minute]);
NSLog(@"[currentDateComponents hour]: %i", [currentDateComponents hour]);
NSLog(@"[currentDateComponents day]: %i", [currentDateComponents day]);
NSLog(@"[currentDateComponents week]: %i", [currentDateComponents week]);
NSLog(@"[currentDateComponents month]: %i", [currentDateComponents month]);
NSLog(@"[currentDateComponents year]: %i", [currentDateComponents year]);
[dateComps setDay: [currentDateComponents day]];
[dateComps setMonth:[currentDateComponents month]];
[dateComps setYear:[currentDateComponents year]];
[dateComps setHour:[currentDateComponents hour]];
[dateComps setMinute:[currentDateComponents minute]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = [itemDate addTimeInterval:-(minutesBefore*60)];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = [NSString stringWithFormat:@"%@\n%@",
streetAddress,
stringOfWhenAuctionIsOn];
localNotif.alertAction = NSLocalizedString(@"View Details", nil);
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:streetAddress
forKey:idOfStreetAlert];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];