2013-01-18 64 views
-1

首先,对不起我的英文。 我遇到了创建多个本地通知的应用程序出现问题。我安排他们在一个for循环:本地通知显示不止一次

for (int i = 0; i < 100 ; i++) { 
    compteur++; 

    UILocalNotification *prototypeNotification = [[UILocalNotification alloc] init]; 
    prototypeNotification.timeZone = [NSTimeZone defaultTimeZone]; 

    prototypeNotification.applicationIconBadgeNumber = 0; 
    prototypeNotification.alertBody = @"ALERTE PILULE !!!!!"; 
    prototypeNotification.alertAction = @"Ok"; 
    [prototypeNotification setSoundName:UILocalNotificationDefaultSoundName]; 
    prototypeNotification.fireDate = itemDate; 

    [[UIApplication sharedApplication] scheduleLocalNotification:prototypeNotification]; 
    [prototypeNotification release]; 

    itemDate = [[NSCalendar currentCalendar] dateByAddingComponents:comps toDate:itemDate options:0]; 


    if (compteur == 21) { 
     compteur = 0; 
     itemDate = [[NSCalendar currentCalendar] dateByAddingComponents:comps2 toDate:itemDate options:0]; 

    } 
} 

当firedate已经到来,它工作得很好,但它有时会显示不只有一个,但2或3或6警报......我只是找不到为什么...有人可以帮助我吗? 谢谢

回答

0
- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 
    // Called as part of the transition from the background to the inactive state; 
    //here you can undo many of the changes made on entering the background. 
    UIApplication *APP =[UIApplication sharedApplication]; 
    NSArray *oldnotification = [APP scheduledLocalNotifications]; 
    if (oldnotification>=0) 
    { 
     [APP cancelAllLocalNotifications]; 
    } 
} 

您是否在上面的模块中添加此代码..要取消旧的通知?

+0

嗯..问题或答案? – kleopatra

+0

@kleopatra这是问题与答案:P,如果你不添加此方法..然后这是答案,如果你添加这个,那么它不是:P –