如何创建多个本地通知 [英] How to create multiple Local Notifications

查看:97
本文介绍了如何创建多个本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create multiple local notifications, in my app, but for some reason only the First Notification Pop's up, the rest just does not work, this is my code.

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

I have a class named criaAlertas, which is responsible for creating the notifications, in that class i have the following method:

-(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;
}

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

So i instantiated a object of that class, and tried to do this:

    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];

我做错了什么?

推荐答案

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

The problem is that you're overwriting all of the local notifications currently scheduled with the call to your -setarNotificacao:nome: function. This line

    myapp.scheduledLocalNotifications = arrayOfNOtifications;

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

sets all of the currently scheduled notifications to arrayOfNotifications; if a notification currently scheduled is not in that array, then it is canceled.

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

The fix is to use the -[UIApplication scheduleLocalNotification:] method to schedule the notification, which adds the given notification without canceling any notifications already scheduled:

[myapp scheduleLocalNotification:notification];

这篇关于如何创建多个本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆