Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create and cancel unique UILocalNotification from a custom class?

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

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

}

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

}

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.

Thanks in advance.

like image 219
Michael Campsall Avatar asked Feb 10 '12 17:02

Michael Campsall


1 Answers

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.

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

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.

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

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

like image 102
NJones Avatar answered Oct 29 '22 20:10

NJones