如何创建多个本地通知

时间:2013-10-31 04:03:38

标签: ios objective-c foundation

我正在尝试在我的应用中创建多个本地通知,但出于某种原因只有第一个通知弹出窗口,其余的只是不起作用,这是我的代码。

我有一个名为 criaAlertas 的类,它负责创建通知,在该类中我有以下方法:

-(void)setarNotificacao:(NSInteger)quando nome:(UILocalNotification *)notification 
{
    UIApplication *myapp = [UIApplication sharedApplication];

    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:quando];
    notification.alertBody = @"Nice";
    notification.timeZone = [NSTimeZone defaultTimeZone];
    notification.soundName = UILocalNotificationDefaultSoundName;

    NSMutableArray *arrayOfNOtifications = [[NSMutableArray alloc]init];

    [arrayOfNOtifications addObject:notification];
    myapp.scheduledLocalNotifications = arrayOfNOtifications;
}

所以我实例化了该类的一个对象,并尝试这样做:

    criaAlertas *novoAlerta = [[criaAlertas alloc]init];
    UILocalNotification *notaUm = [[UILocalNotification alloc]init];
    UILocalNotification *notaDois = [[UILocalNotification alloc]init];
    [novoAlerta setarNotificacao:15 nome:notaUm];
    [novoAlerta setarNotificacao:30 nome:notaDois];

我做错了什么?

1 个答案:

答案 0 :(得分:6)

问题是您通过调用-setarNotificacao:nome:函数覆盖当前安排的所有本地通知。这一行

    myapp.scheduledLocalNotifications = arrayOfNOtifications;

将所有当前安排的通知设置为arrayOfNotifications;如果当前安排的通知不在该数组中,则会被取消。

修复方法是使用-[UIApplication scheduleLocalNotification:]方法安排通知,这会添加给定的通知而不取消已安排的任何通知:

[myapp scheduleLocalNotification:notification];
相关问题