2015-06-27 3 views
0

Я хочу иметь 5 местных уведомлений каждый день, и вот код, который я использовал для запуска локального уведомления, все круто, но если первое уведомление выходит, и если я запустил APP переднего плана, то или другое уведомление не выйдет. Пожалуйста, помогите сохранить все уведомления, даже если я запустил APP на переднем плане.5 местных уведомлений каждый день

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

    NSCalendar *c1 = [NSCalendar currentCalendar]; // gets default calendar 

    NSDateComponents *co1 = [c1 components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date 

    [co1 setHour:15]; 

    [co1 setMinute:54]; 

    n1.fireDate = [c1 dateFromComponents:co1]; 

    n1.alertBody = @"3:54"; 

    n1.soundName = @"alarm.wav"; 

    n1.timeZone = [NSTimeZone defaultTimeZone]; 

    n1.alertAction= @"3:54"; 

    [[UIApplication sharedApplication] scheduleLocalNotification: n1]; 

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

    NSCalendar *c2 = [NSCalendar currentCalendar]; // gets default calendar 

    NSDateComponents *co2 = [c2 components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date 

    [co2 setHour:15]; 

    [co2 setMinute:55]; 

    n2.fireDate = [c2 dateFromComponents:co2]; 

    n2.alertBody = @"3:55"; 

    n2.soundName = @"alarm.wav"; 

    n2.timeZone = [NSTimeZone defaultTimeZone]; 

    n2.alertAction= @"3:55"; 

    [[UIApplication sharedApplication] scheduleLocalNotification: n2]; 

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

    NSCalendar *c3 = [NSCalendar currentCalendar]; // gets default calendar 

    NSDateComponents *co3 = [c3 components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date 


    [co3 setHour:15]; 

    [co3 setMinute:56]; 

    n3.fireDate = [c3 dateFromComponents:co3]; 

    n3.alertBody = @"3:56"; 

    n3.soundName = @"alarm.wav"; 

    n3.timeZone = [NSTimeZone defaultTimeZone]; 

    n3.alertAction = @"3:56"; 

    [[UIApplication sharedApplication] scheduleLocalNotification: n3]; 

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

    NSCalendar *c4 = [NSCalendar currentCalendar]; // gets default calendar 

    NSDateComponents *co4 = [c4 components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date 

    [co4 setHour:15]; 

    [co4 setMinute:57]; 

    n4.fireDate = [c4 dateFromComponents:co4]; 

    n4.alertBody = @"3:57"; 

    n4.soundName = @"alarm.wav"; 

    n4.timeZone = [NSTimeZone defaultTimeZone]; 

    n4.alertAction = @"3:57"; 

    [[UIApplication sharedApplication] scheduleLocalNotification: n4]; 

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

    NSCalendar *c5 = [NSCalendar currentCalendar]; // gets default calendar 

    NSDateComponents *co5 = [c5 components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date 

    [co5 setHour:15]; 

    [co5 setMinute:58]; 

    n5.fireDate = [c5 dateFromComponents:co5]; 

    n5.alertBody = @"3:58"; 

    n5.soundName = @"alarm.wav"; 

    n5.timeZone = [NSTimeZone defaultTimeZone]; 

    n5.alertAction = @"3:58"; 

    [[UIApplication sharedApplication] scheduleLocalNotification: n5]; 

ответ

2

Если приложение в foreground уведомление не представлено, но это ниже метод будет называться,

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
{ 
} 

Сначала вы должны определить уведомление по userinfo прикрепленную в нем. (Если у вас нет дайте, дайте userinfo каждому уведомлению.)

При возникновении вызова в этом методе подавайте предупреждение.

Смежные вопросы