0

我正在安排两个通知,如下所示。该应用程序是一个长期存在的应用程序。一个本地通知计划每小时运行一次。另一个计划每天运行一次。只有第二个预定通知(每小时通知)会触发。

- (void)scheduleNotification
{
LogInfo(@"IN scheduleNotification - DELETEYESTERDAY NOTIFICATION SCHEDULED.");

UILocalNotification *notif = [[UILocalNotification alloc] init];

NSDictionary *deleteDict = [NSDictionary dictionaryWithObject:@"DeleteYesterday"
                                                          forKey:@"DeleteYesterday"];

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];

components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];

NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];

[components setDay: day];
[components setMonth: month];
[components setYear: year];
[components setHour: 00];
[components setMinute: 45];
[components setSecond: 0];
[calendar setTimeZone: [NSTimeZone systemTimeZone]];
NSDate *dateToFire = [calendar dateFromComponents:components];

notif.fireDate = dateToFire;
notif.timeZone = [NSTimeZone systemTimeZone];
notif.repeatInterval = NSDayCalendarUnit;
notif.userInfo = deleteDict;

[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}

然后我在上面安排这个:

- (void)scheduleHeartBeat
{
LogInfo(@"IN scheduleHeartBeat - HEARTBEAT NOTIFICATION SCHEDULED.");

UILocalNotification *heartbeat = [[UILocalNotification alloc] init];

NSDictionary *heartbeatDict = [NSDictionary dictionaryWithObject:@"HeartBeat"
                                                     forKey:@"HeartBeat"];

heartbeat.userInfo = heartbeatDict;

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];

components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];

NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];

[components setDay: day];
[components setMonth: month];
[components setYear: year];
[components setHour: 00];
[components setMinute: 50];
[components setSecond: 0];
[calendar setTimeZone: [NSTimeZone systemTimeZone]];
NSDate *dateToFire = [calendar dateFromComponents:components];

heartbeat.fireDate = dateToFire;
heartbeat.timeZone = [NSTimeZone systemTimeZone];
heartbeat.repeatInterval = NSHourCalendarUnit;

[[UIApplication sharedApplication] scheduleLocalNotification:heartbeat];
}

以上是在应用程序启动时在主视图控制器的 viewDidLoad 中安排的。

- (void)viewDidLoad
{
[self scheduleNotification];
[self scheduleHeartBeat];

[super viewDidLoad];
//OTHER CODE HERE
}

然后在 appdelegate 我有以下内容:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
LogInfo(@"IN didReceiveLocalNotification NOTIFICATION RECEIVED.");

NSString *notificationHeartBeat = nil;
NSString *notificationDeleteYesterday = nil;

application.applicationIconBadgeNumber = 0;    

if (notification) {
    notificationHeartBeat = [notification.userInfo objectForKey:@"HeartBeat"];
    notificationDeleteYesterday = [notification.userInfo objectForKey:@"DeleteYesterday"];
    LogInfo(@"IN didReceiveLocalNotification HEARTBEAT NOTIFICATION TYPE: %@", notificationHeartBeat);
    LogInfo(@"IN didReceiveLocalNotification DELETEYESTERDAY NOTIFICATION TYPE: %@", notificationDeleteYesterday);
}

if ([notificationHeartBeat isEqualToString:@"HeartBeat"]) {
    //CREATE THE HEARTBEAT
    LogInfo(@"CREATING THE HEARTBEAT.");
    //CALL THE FUNCTIONALITY HERE THAT CREATES HEARTBEAT.
}

if ([notificationDeleteYesterday isEqualToString:@"DeleteYesterday"]) {
    //DELETE YESTERDAYS RECORDS
    LogInfo(@"DELETING YESTERDAYS RECORDS.");

}    
}

最后安排的通知 (scheduleHeartBeat) 是唯一触发的通知。有人可以帮我弄清楚为什么会这样吗?

4

1 回答 1

1

您已将重复间隔指定为NSDayCalendarUnit。因此,您的通知将在第二天的指定时间触发。

出于测试目的更改此重复间隔并检查您的代码是否正常工作。

我已经测试过了。您的代码在这里正常工作。

于 2013-06-27T10:16:14.637 回答