iPhone - 从 UILocalNotification 获取剩余时间

发布于 2024-11-04 21:01:09 字数 610 浏览 4 评论 0原文

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

假设我做了这样的事情:

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

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

提前致谢。

I want to know if there's a way to get the remaining time from a Local Notification previously set.

Let's say I make something like this:

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

So then I fully close my app. I reopen it and I want to show some message saying how much time is left for the notification to launch.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九公里浅绿 2024-11-11 21:01:09

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

You are supposed to be able to get a list of the currently scheduled notifications with the UIApplication scheduledLocalNotifications property. Maybe get this and do some math on the time?

生来就爱笑 2024-11-11 21:01:09

当您调用以下方法时,如 [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;
     }
}

When you call the below method like [self timeRemaning]; it will give you how many seconds renaming if there is any notification registered. This will work only for one registered notification.

-(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;
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文