Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Performing Operations Upon A Local Notification Being Fired

Hello, all. As you guessed I am having some problems performing some code after a notification has been fired. I tried using the appDelegate method didReceiveLocalNotification but that code is performed immediately after the notification has been scheduled. What do I do to solve this problem? Thank you!

2 Answers

if you are using NSNotificationCenter you can subscribe to the notification.

first you post the notification you want the subscribing objects to be notified of

//in some class where you post the notification
 [[NSNotificationCenter defaultCenter] 
        postNotificationName:@"TestNotification" 
        object:self];

then you can subscribe to that specific notification in some other class

- (void)viewDidLoad {

    [super viewDidLoad];

[[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(receiveTestNotification) 
        name:@"TestNotification"
        object:nil];

}

- (void)receiveTestNotification {

NSLog(@"notification received");

}

so whenever your notification gets posted, the subscribing object is notified of that and it runs the code specified in the selector.

also, you need to be sure and unsubscribe from the notification in the dealloc method

[[NSNotificationCenter defaultCenter] removeObserver:self];

But if you're not using the notification center, how would you do things when a local notification has been fired? Is there some method that you can use for this or any other way?