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

10,995

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.

Share:
10,995
Michael Campsall
Author by

Michael Campsall

Co-founder of Watermelon App Works and an educator working in community development.

Updated on June 11, 2022

Comments

  • Michael Campsall
    Michael Campsall about 2 years

    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.

  • Michael Campsall
    Michael Campsall over 12 years
    Thanks so much once again! Very clear explanation, and a solid code example. Everything makes sense and it works like a charm. One thing I was curious about however was why it was necessary to copy cheduledNotifications.
  • NJones
    NJones over 12 years
    @jemicha Glad I could help. To answer your question as to why it was necessary to copy the scheduledLocalNotifications, it's not. This is an example of my extreme paranoia. If apple ever exposes it's internal NSMutableArray of notifications, either through a policy change (so unlikely it's laughable) or a bug, then this copy would avoid possible problems. But again if you leave it out you will likely never see a difference.
  • Supertecnoboff
    Supertecnoboff over 8 years
    Where you put dictionaryWithObject:self.name - how do you change self.name to a unique ID?