检测将要触发的下一个UILocalNotification [英] Detect next UILocalNotification that will fire

查看:107
本文介绍了检测将要触发的下一个UILocalNotification的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法找到 NSDate 下一个会触发的本地通知?

Is there a way to find the NSDate for the next local notification that will fire?

,我已设置了三个本地通知:

For example, I've set up three local notifications:

通知#1:设置为昨天下午3:00发射,重复间隔为每天。

Notification #1: Set to fire yesterday at 3:00 PM with repeat interval of daily.

通知#2:设置为今天下午5:00发射,重复间隔为每天。

Notification #2: Set to fire today at 5:00 PM with repeat interval of daily.

通知#3:明天下午6:00,重复间隔每日。

Notification #3: Set to fire tomorrow at 6:00 PM with repeat interval of daily.

现在是下午4:00,下一个本地通知将触发通知#2。

Given that is is currently 4:00 PM, the next local notification that will fire is notification #2.

如何检索此本地通知并获取其日期?

How can I retrieve this local notification and get its date?

我知道我可以检索这些本地通知

I know that I can retrieve these local notifications in an array, but how do I get the next one that will fire based on today's date?

推荐答案

数组中的通知,但是如何获取下一个将根据今天的日期触发的你的任务是确定每个通知给定的
日期后的下一个消防日期。
UILocalNotification NSLog()输出显示下一个激活日期
,但不幸的是它似乎不可用作(公共)属性。

The main goal for your task is to determine the "next fire date" after a given date for each notification. The NSLog() output of a UILocalNotification shows this next fire date, but unfortunately it seems not to be available as a (public) property.

我已经从http://stackoverflow.com/a/18730449/1187415 (小型
改进),并将其改写为 UILocalNotification
(这不是完美的,不包括分配给通知的
时区。)

I have taken the code from http://stackoverflow.com/a/18730449/1187415 (with small improvements) and rewritten that as a category method for UILocalNotification. (This is not perfect. It does not cover the case that a time zone has been assigned to the notification.)

@interface UILocalNotification (MyNextFireDate)
- (NSDate *)myNextFireDateAfterDate:(NSDate *)afterDate;
@end

@implementation UILocalNotification (MyNextFireDate)
- (NSDate *)myNextFireDateAfterDate:(NSDate *)afterDate
{
    // Check if fire date is in the future:
    if ([self.fireDate compare:afterDate] == NSOrderedDescending)
        return self.fireDate;

    // The notification can have its own calendar, but the default is the current calendar:
    NSCalendar *cal = self.repeatCalendar;
    if (cal == nil)
        cal = [NSCalendar currentCalendar];

    // Number of repeat intervals between fire date and the reference date:
    NSDateComponents *difference = [cal components:self.repeatInterval
                                               fromDate:self.fireDate
                                                 toDate:afterDate
                                                options:0];

    // Add this number of repeat intervals to the initial fire date:
    NSDate *nextFireDate = [cal dateByAddingComponents:difference
                                                     toDate:self.fireDate
                                                    options:0];

    // If necessary, add one more:
    if ([nextFireDate compare:afterDate] == NSOrderedAscending) {
        switch (self.repeatInterval) {
            case NSDayCalendarUnit:
                difference.day++;
                break;
            case NSHourCalendarUnit:
                difference.hour++;
                break;
            // ... add cases for other repeat intervals ...
            default:
                break;
        }
        nextFireDate = [cal dateByAddingComponents:difference
                                            toDate:self.fireDate
                                           options:0];
    }
    return nextFireDate;
}
@end



使用它,根据
下一个日期的通知:

Using that, you can sort an array of local notifications according to the next fire date:

NSArray *notifications = @[notif1, notif2, notif3];

NSDate *now = [NSDate date];
NSArray *sorted = [notifications sortedArrayUsingComparator:^NSComparisonResult(UILocalNotification *obj1, UILocalNotification *obj2) {
    NSDate *next1 = [obj1 myNextFireDateAfterDate:now];
    NSDate *next2 = [obj2 myNextFireDateAfterDate:now];
    return [next1 compare:next2];
}];

现在 sorted [0] 下一个通知。

这篇关于检测将要触发的下一个UILocalNotification的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆