本地通知显示不止一次

时间:2013-01-18 16:55:11

标签: iphone ios objective-c xcode uilocalnotification

首先,对不起我的英语。 我有一个创建多个本地通知的应用程序的问题。我在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];

    }
}

当火药来了,它运作良好但它不仅显示一个,而且有时显示2或3或6个警报......我只是找不到原因......有人可以帮助我吗? 感谢

1 个答案:

答案 0 :(得分: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];
    }
}

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