Iphone UILocalNotification计划警报
我安排了一个通知,并在它显示警报消息之前给它60分钟的警告 一旦我添加通知,我的应用程序委托中的方法将被调用:Iphone UILocalNotification计划警报,iphone,uilocalnotification,Iphone,Uilocalnotification,我安排了一个通知,并在它显示警报消息之前给它60分钟的警告 一旦我添加通知,我的应用程序委托中的方法将被调用: - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 我怎么知道它会以警报的形式出现在后台?是否需要重写或使用任何其他委托方法,以确保给定间隔为60分钟的计划警报 - (void)scheduleNotificat
- (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];
没有用于后台的委托方法,您需要在didFinishLaunchingWithOptions中编写代码
UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
//your code
NSLog(@"Recieved Notification %@",localNotif);
}
当应用程序从后台获取通知时,它会创建您的逻辑。不要担心应用程序获取通知。您可以设置通知。如果通知立即显示,您的fireDate属性可能有问题。看起来您正在尝试验证发送的日期是否正确,但您还应该验证fireDate是否符合预期 另外,你也可以这样做
localNotif.fireDate = [item dateByAddingTimeInterval:-60*minutesBefore];
为了达到同样的效果,不必乱搞NSDateComponents
。我还没有测试过这个,但它可能会工作
至于当计时器启动而你的应用程序没有运行时会发生什么。如果应用程序尚未终止
应用程序:didReceiveLocalNotification:
将被调用。如果在另一端,您的应用程序已被终止,您需要签入@Ishu指出的application:didfishlaunchingwithoptions:
。我通常只是将通知传递给一个常用方法,以避免重复代码。只是好奇,为什么要将间隔设置为负值\