Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7550251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:09:53+00:00 2026-05-30T10:09:53+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:09:54+00:00Added an answer on May 30, 2026 at 10:09 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have this code #include <iostream> #include <curl.h> #include <windows.h> #include boost\timer.hpp int
I have created a Alarm using AlarmManager. Intent intent = new Intent(MyApp.this,NotificationMessage.class); PendingIntent sender
I have a database that is currently sharded. This is the first time that
Currently, we use a giant configuration object that is serialized to/from XML. This has
In My application i have set the many Alarm Notification. All are at different
I have an application in which i have set a local notification in my
I followed the instructions in this thread to create a daily alarm that starts
I currently have a timer that ticks and changes the label to count down
I have a timer app and I want to vibrate the phone once the
I currently have some C code that uses sigaction to associate a handler to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.