2013-01-18 61 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 –