iPhone - 从UILocalNotification获取剩余时间

时间:2011-05-03 20:31:48

标签: iphone notifications push-notification 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 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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;
     }
}