1

我想知道是否有办法从先前设置的本地通知中获取剩余时间。iPhone - 从UILocalNotification获取剩余时间

比方说,我做这样的事情:

//Creating Notification 
    UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
    localNotif.fireDate = someTime; 
    localNotif.alertBody = @"Your parking time will expire in 10 mins"; 
    localNotif.alertAction = @"View"; 
    localNotif.soundName = UILocalNotificationDefaultSoundName; 
    localNotif.applicationIconBadgeNumber = 1; 

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
    [localNotif release]; 

于是我完全关闭我的应用程序。我重新打开它,我想显示一些消息,说明通知启动需要多长时间。

在此先感谢。

回答

2

您应该能够使用UIApplication scheduledLocalNotifications属性获取当前计划的通知的列表。也许得到这个,并在这段时间做一些数学?

+0

因此,如果我使用scheduledLocalNotifications检索我的通知,我可以得到它将触发的时间? – Chompas 2011-05-03 20:45:23

+0

Docs说它应该返回一个UILocalNotifications数组。该类具有fireDate属性,其中包含“系统应发送通知的日期和时间”。从中减去当前时间应该告诉你剩下的时间。 – DavidN 2011-05-03 20:58:24

0

当你调用下面的方法如[self timeRemaning];它会给你多少秒重命名,如果有任何注册通知。这只适用于一个注册通知。

-(int)timeRemaning{ 
    NSArray *checkNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications]; 
    if (![checkNotifications count] == 0) { 
    UILocalNotification *localNotification = [checkNotifications objectAtIndex:0]; 
    NSString* notifdate=[localNotification.fireDate description]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss ZZ"]; 
    NSCalendar *c = [NSCalendar currentCalendar]; 
    NSDate *d1 = [NSDate date]; 
    NSDate *d2 = [dateFormatter dateFromString:notifdate]; 
    NSDateComponents *components = [c components:NSSecondCalendarUnit fromDate:d1 toDate:d2 options:0]; 
    NSInteger diff = components.second; 
     int temp = diff; 
    return temp; 

    }else{ 
     return 0; 
    } 
}