如何从自定义类创建和取消唯一的UILocalNotification? [英] How do I create and cancel unique UILocalNotification from a custom class?

查看:97
本文介绍了如何从自定义类创建和取消唯一的UILocalNotification?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有一个带警报的计时器(本地通知)。

Currently I have a timer with an alarm (local notification).

我想从这段代码创建一个计时器类来创建多个计时器和通知(最多5)我正在努力学习如何使用类方法创建和取消唯一通知。

I want to create a timer class from this code to create multiple timers and notifications (at most 5) and I am struggling with how to create and cancel unique notifications with a class method.

- (UILocalNotification *) startAlarm {

    [self cancelAlarm]; //clear any previous alarms

    alarm = [[UILocalNotification alloc] init];
    alarm.alertBody = @"alert msg"
    alarm.fireDate = [NSDate dateWithTimeInterval: alarmDuration sinceDate: startTime]; 
    alarm.soundName = UILocalNotificationDefaultSoundName; 

    [[UIApplication sharedApplication] scheduleLocalNotification:alarm];

}

我的假设是,如果我有一个创建的类方法一个名为闹钟的UILocalNotification iOS会将所有通知视为相同的通知,并且以下方法将无法按我希望的方式运行:

My assumption is that if I have a class method that creates a UILocalNotification called "alarm" iOS will see all of the notifications as being the same notification and the following method will not function the way I want it to:

- (void)cancelAlarm {

    if (alarm) {    
        [[UIApplication sharedApplication] cancelLocalNotification:alarm];
    }

}

所以我需要一种方法来命名这些UILocalNotifications因为它们被创建,例如alarm1 alarm2 ... alarm5所以我可以取消正确的。

So I need a way to name these UILocalNotifications as they are created e.g. alarm1 alarm2...alarm5 so I can cancel the right one.

提前致谢。

推荐答案

问题的答案在于每个 UILocalNotification 都有的 userInfo 字典参数。您可以在此词典中设置键的值以标识通知。

The answer to your problem lies in the userInfo dictionary parameter that every UILocalNotification has. You can set values for keys in this dictionary to identify the notification.

要轻松实现此功能,您只需让您的计时器类具有 NSString name属性。并使用一些类宽字符串作为该值的键。以下是基于您的代码的基本示例:

To implement this easily all you have to do is have your timer class have an NSString "name" property. And use some class wide string for the key for that value. Here is a basic example based on your code:

#define kTimerNameKey @"kTimerNameKey"

-(void)cancelAlarm{
    for (UILocalNotification *notification in [[[UIApplication sharedApplication] scheduledLocalNotifications] copy]){
        NSDictionary *userInfo = notification.userInfo;
        if ([self.name isEqualToString:[userInfo objectForKey:kTimerNameKey]]){
            [[UIApplication sharedApplication] cancelLocalNotification:notification];
        }
    }
}
-(void)scheduleAlarm{
    [self cancelAlarm]; //clear any previous alarms
    UILocalNotification *alarm = [[UILocalNotification alloc] init];
    alarm.alertBody = @"alert msg";
    alarm.fireDate = [NSDate dateWithTimeInterval:alarmDuration sinceDate:startTime]; 
    alarm.soundName = UILocalNotificationDefaultSoundName; 
    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:self.name forKey:kTimerNameKey];
    alarm.userInfo = userInfo;
    [[UIApplication sharedApplication] scheduleLocalNotification:alarm];
}

此实现应该是相对自我解释的。基本上,当一个timer类的实例调用了 -scheduleAlarm 并且它正在创建一个新的通知时,它将它的字符串属性name设置为 kTimerNameKey 。因此,当此实例调用 -cancelAlarm 时,它会枚举通知数组,以查找包含该键名称的通知。如果找到它就删除它。

This implementation should be relatively self explanatory. Basically when an instance of the timer class has -scheduleAlarm called and it is creating a new notification it sets it's string property "name" as the value for the kTimerNameKey. So when this instance calls -cancelAlarm it enumerates the array of notifications looking for a notification with it's name for that key. And if it finds one it removes it.

我想你的下一个问题是如何给你的每个计时器name属性一个唯一的字符串。因为我碰巧知道你正在使用IB来实例化它们(从你关于此事的其他问题),你可能会在 viewDidLoad 中执行此操作:

I imagine your next question will be how to give each of your timers name property a unique string. Since I happen to know you are using IB to instantiate them (from your other question on the matter) you would likely do this in viewDidLoad something like:

self.timerA.name = @"timerA";
self.timerB.name = @"timerB";

您还可以将name属性与您可能拥有的标题标签绑定。

You could also tie the name property in with a title label you may have.

这篇关于如何从自定义类创建和取消唯一的UILocalNotification?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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