应用程序关闭7天后安排本地通知

为我的应用程序启用local notifications要相当困难。

是的,我设置了一个local notification项目,我想在应用程序关闭一周后安排。 例如,如果用户在星期六8日打开应用程序,则本地通知应出现在15日的下一个星期六,但时间应该更改。 例如,他/她在晚上8点关闭应用程序,我不想在下周8点打扰他们,所以我想在下午6点或类似的地方显示每个通知。

现在你知道我的问题,这是我正在使用的代码:

 - (void)applicationDidEnterBackground:(UIApplication *)application { [[UIApplication sharedApplication] cancelAllLocalNotifications]; NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ; NSDateComponents *componentsForReferenceDate = [[NSDateComponents alloc] init]; //set day (saturday) [componentsForReferenceDate setDay:26] ; [componentsForReferenceDate setMonth:1] ; [componentsForReferenceDate setYear:2013] ; [componentsForReferenceDate setHour: 17] ; [componentsForReferenceDate setMinute:30] ; [componentsForReferenceDate setSecond:00] ; NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForReferenceDate]; // Create the notification UILocalNotification *notification = [[UILocalNotification alloc] init] ; notification.fireDate = fireDateOfNotification ; notification.timeZone = [NSTimeZone localTimeZone] ; notification.alertBody = [NSString stringWithFormat: @"Du wirst vermisst! \nDeine App braucht dich, schreibe sie zu Ende!"] ; notification.alertAction = @"Zurückkehren"; notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Some information"] forKey:@"information"]; notification.repeatInterval= NSWeekCalendarUnit ; notification.soundName = @"Appnotifisound.wav"; notification.applicationIconBadgeNumber = 1; [[UIApplication sharedApplication] scheduleLocalNotification:notification] ; } 

我知道必须有更多的方法来删除徽章和didrecievenotification ,但我让他们出去,因为这并不重要。

使用此代码,我设法在每个星期六下午5:30 (德国)安排通知。 但我只想安排一次,当应用程序关闭一个星期。 这有点可能吗? 如果有人能纠正我的代码或给我一个解决方案,我会很高兴。

致以最诚挚的问候,感谢您阅读这篇长篇文章,

诺亚

您应该取消预先安排以前的通知。

 for (UILocalNotification *lNotification in [[UIApplication sharedApplication] scheduledLocalNotifications]) { if ([[lNotification.userInfo valueForKey:@"information"] isEqualToString:@"Some information"]) { [[UIApplication sharedApplication]cancelLocalNotification:lNotification]; break; } } 

通过以下方式设置开火日期:

 NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *componentsForReferenceDate = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate date]]; [componentsForReferenceDate setHour: 17] ; [componentsForReferenceDate setMinute:30] ; [componentsForReferenceDate setSecond:00] ; NSDate *tempDate = [calendar dateFromComponents: componentsForReferenceDate]; [componentsForReferenceDate release]; NSDateComponents *comps = [[NSDateComponents alloc]init]; [comps setDay:7]; NSDate *fireDateOfNotification = [calendar dateByAddingComponents:comps toDate:tempDate options:0] [comps release]; 

notification.repeatInterval = NSWeekCalendarUnit导致了这个问题。 使用:

 notification.repeatInterval = 0; 

这可以防止重复( 来源 )。

这是Swift 2.0改编版

取消安排当前通知

  for notifMe:AnyObject in UIApplication.sharedApplication().scheduledLocalNotifications!{ let title = notifMe.userInfo["information"] as? String if title == "some information"{ UIApplication.sharedApplication().cancelLocalNotification(notifMe as! UILocalNotification) } } 

设置通知的日期

  let date = NSDate() let calendar = NSCalendar.currentCalendar() let components = calendar.components(NSCalendarUnit.Year.union(NSCalendarUnit.Month).union(NSCalendarUnit.Day),fromDate: date) components.hour = 17 components.minute = 30 components.second = 00 let tempDate = calendar.dateFromComponents(components)! let comps = NSDateComponents() comps.day = 7 let fireDateOfNotification = calendar.dateByAddingComponents(comps, toDate: tempDate, options:[])