2013-06-27 42 views
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)是炒魷魚,通知。 有人能幫我弄清楚爲什麼會發生這種情況嗎?

回答

1

您已將您的重複間隔指定爲NSDayCalendarUnit。因此,您的通知將在指定時間的第二天啓動。

爲了測試目的,改變你的這個重複間隔,並檢查你的代碼是否正常工作。

我測試過了。你的代碼在這裏正常工作。