1

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

假设我做了这样的事情:

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

所以我完全关闭了我的应用程序。我重新打开它,我想显示一些消息,说明通知启动还剩多少时间。

提前致谢。

4

2 回答 2

2

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

于 2011-05-03T20:37:35.180 回答
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;
     }
}
于 2014-07-23T10:58:46.720 回答