2013-05-05 156 views
3

我试图对我的本地通知进行有条件的启动时间,但是当我尝试这两种方式时,它没有失败,但仍然无法工作。所以,我想知道我是否能够做这样的事情?是否可以为UILocalNotification设置Conditioned`firedate`?

注意: 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] ; 


}} 

OR

-(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] ; 


}} 

感谢

如果没有,会是什么做出这样的条件最简单的方法?

+0

你记录了火灾日期吗?如果使用组件中的分钟,则需要在从日历+日期(NSMinuteCalendarUnit)中获取组件时加载分钟。 – Wain 2013-05-05 06:13:24

+0

是的,我没有记录它,它永远不会进入if条件 – user1949873 2013-05-05 11:38:08

+0

尝试'if([fire timeIntervalSinceDate:startTime.date]> = 0)//火在开始之前。 – 2013-05-07 09:58:13

回答

0

使用的NSDate Compare:方法,而不是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 
} 

也看看链接此类似,你需要link

+0

不幸的是,通知仍然是射击,它并没有取消!我听说你不能对'UILocalNotification'有任何限制吗? – user1949873 2013-05-07 22:19:00

+0

请告诉你究竟需要做什么,什么是不工作,我的保证:startTime和EndTime是u通知不会被触发的时间。对。??根据这段代码,如果fireDate在开始和结束时间之间,Notification将不会被创建,所以它不可能触发。你是否想要取消所有已经创建的,在DoNotDisturb时间范围内的通知?或者你只是不希望用户允许在DoNotDisturb时间框架之间创建新的UILocalnotication – Bonnie 2013-05-08 05:56:56

+0

你的假设是正确的。我想取消startTime和endTime之间的通知。那么,这段代码是否能够在后台取消所有本地通知?因为我试过了,它不起作用! – user1949873 2013-05-08 07:26:55

0

之间有什么什么,我,会尝试做做的是,睡眠时间开始时,调用一个方法如:goingToSleep:,你可以使用 dispatch_laterperformSelector: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]; 
    } 
    

在此之后休眠时间结束 ,再次调用一个方法例如该通知}:wakingUp:

然后在wakingUp:

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

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

编辑To have long Running Appicaions

+0

非常感谢代码部分工作。我尝试了延迟属性,它在应用程序运行时工作。但是,我仍然试图理解Plist,NSDictionary和数组,所以我可以使它在后台工作。我有两个问题:你有任何指南的链接解释你的答案?或者你能指出我详细的答案吗? – user1949873 2013-05-11 10:00:12

+0

你是指“如果应用程序不是内存,一个问题可以执行这些方法”是什么意思?什么是内存应用程序? – user1949873 2013-05-11 10:01:51

+0

不幸的是,performSelctor:withObject:afterDelay在第一个10分钟后将不会在后台工作:/ – user1949873 2013-05-11 16:22:37

相关问题