3

我正在尝试为我的本地通知设置一个有条件的触发时间,但是当我尝试这两种方法时,它并没有失败,但它仍然没有用。所以,我想知道我是否能够做这样的事情?

注意: startTime 和 endTime 是来自日期选择器的时间。

-(void) TEST {

NSCalendar *calendar = [NSCalendar currentCalendar];
calendar.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];

NSDateComponents *components = [calendar components:NSHourCalendarUnit fromDate:[NSDate date]];
components.hour   = 3;
components.minute = (components.minute + 1) % 60;
components.second = 57;

NSDate *fire = [calendar dateFromComponents:components];
UILocalNotification *notif = [[UILocalNotification alloc]  init] ;
if (notif == nil)
    return;

if (fire < startTime.date) {


notif.fireDate =fire ;
    notif.repeatInterval= NSMinuteCalendarUnit ;
    notif.alertBody = [NSString stringWithFormat: @"You are missed!"] ;
    [[UIApplication sharedApplication] scheduleLocalNotification:notif] ;


    }

if (fire > endTime.date) {


    notif.fireDate =fire ;
    notif.repeatInterval= NSMinuteCalendarUnit ;
    notif.alertBody = [NSString stringWithFormat: @"You are missed!"] ;
    [[UIApplication sharedApplication] scheduleLocalNotification:notif] ;


}}

或者

-(void) TEST {

NSCalendar *calendar = [NSCalendar currentCalendar];
calendar.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];

NSDateComponents *components = [calendar components:NSHourCalendarUnit fromDate:[NSDate date]];
components.hour   = 3;
components.minute = (components.minute + 1) % 60;
components.second = 57;

NSDate *fire = [calendar dateFromComponents:components];
UILocalNotification *notif = [[UILocalNotification alloc]  init] ;
if (notif == nil)
    return;

if (fire > startTime.date & fire < endTime.date) {

    [[UIApplication sharedApplication] cancelAllLocalNotifications] ;
}

else {
    notif.fireDate =fire ;
    notif.repeatInterval= NSMinuteCalendarUnit ;
    notif.alertBody = [NSString stringWithFormat: @"You are missed!"] ;
    [[UIApplication sharedApplication] scheduleLocalNotification:notif] ;


}}

谢谢

如果不是,那么产生这种条件的最简单方法是什么?

4

2 回答 2

0

使用 NSDateCompare:方法而不是if(fire > startTime.date)

if([fire compare: startTime.date] == NSOrderedDescending) // if start is later in time than end
{
    // do something
}

从类参考:

If:
The receiver and anotherDate are exactly equal to each other, NSOrderedSame.
The receiver is later in time than anotherDate, NSOrderedDescending.
The receiver is earlier in time than anotherDate, NSOrderedAscending.

所以根据你的情况

if (fire > startTime.date & fire < endTime.date) {

    [[UIApplication sharedApplication] cancelAllLocalNotifications] ;
}

您可以使用

if([fire compare: startTime.date] == NSOrderedDescending && [fire compare: endTime.date]== NSOrderedAescending)
{
   [[UIApplication sharedApplication] cancelAllLocalNotifications] ;
}

如果在选定的开始日期和结束日期之间发生火灾,这将取消所有本地通知

else {
// Fire the notifications
}

也看看链接这类似于你需要的链接

于 2013-05-07T10:09:59.377 回答
0

我会尝试做的是,当睡眠时间开始时,调用一个方法,例如:goingToSleep:,你可以使用 dispatch_lateror performSelector:withObject:afterDelay:

goingToSleep:

NSArray* localNotifications = [[UIApplication sharedApplication] 
                                              scheduledLocalNotifications];
for (UILocalNotification *notification in localNotifications)
{
    if([fire compare: startTime.date] == NSOrderedDescending &&
       [fire compare: endTime.date]== NSOrderedAescending)
    {

这是重要的部分

  • 获取通知的所有属性
  • 创建这些属性的 NSDictionary
  • 将 NSDictionary 添加到数组
  • 并将该数组保存到 plist 中。( Save Array to Plist )

  • 然后使用取消该通知

    [[UIApplication sharedApplication] cancelLocalNotification:notification];
    }
    

}

然后在 SleepTime 结束后,再次调用一个方法,例如:wakingUp:

然后在wakingUp:

  • 读取我们保存的 NSDictionary 数组。
  • 再次使用相同的属性创建 UILocalnotifications。

这样,在睡眠期间不会触发 UILocalNotifications。如果应用程序不是内存,则一个问题可能是执行这些方法。

编辑 长期运行 Appicaions

于 2013-05-10T13:21:47.213 回答