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; 
    } 
}