【问题标题】:iPhone - Get Remaining time from UILocalNotificationiPhone - 从 UILocalNotification 获取剩余时间
【发布时间】:2011-05-03 20:31:48
【问题描述】:

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

假设我做了这样的事情:

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

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

提前致谢。

【问题讨论】:

    标签: iphone notifications push-notification uilocalnotification


    【解决方案1】:

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

    【讨论】:

    • 因此,如果我使用 scheduleLocalNotifications 检索通知,我可以获得触发时间?
    • Docs 说它应该返回一个 UILocalNotifications 数组。该类有一个 fireDate 属性,其中包含“系统应发送通知的日期和时间”。从中减去当前时间应该告诉您剩余时间。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2016-07-10
      • 2018-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多