Crystal
Crystal

Reputation: 29518

How to keep track of UILocalNotifications currently scheduled

MyObject : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSArray *notificationsArray;

I have an array of MyObjects in my tableView that can be edited for name, time of the notification, etc. Currently, I have it setup so when the user presses Save, the current MyObject gets saved to my DataManager's myObjectArray.

DataManager : NSObject
@property (nonatomic, strong) NSMutableArray *myObjectArray;

I call my method in the DataManager to loop through that MyObject instance to schedule the notifications for that MyObject.

I think this would be ok, until the user clicks on one of the MyObjects to edit the time and then I need to reschedule the notifications for that object only. I know you can get

[[UIApplication sharedApplication] scheduledNotifications];

But from this, I don't know which notifications went for which object. So in this scenario, is it better to cancel all notifications for my entire app, and then loop through myObjectArray in the DataManager, for each MyObject instance, and schedule the notifications for each object that way?

Thanks!

Upvotes: 1

Views: 910

Answers (2)

danh
danh

Reputation: 62676

You can associate custom data with your notification using the userInfo property. Build it like this:

UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.userInfo = [NSDictionary dictionaryWithObject:@"myNotificationName" forKey:@"name"];

Then look it up like this:

 - (UILocalNotification *)notificationNamed:(NSString *)name {

    for (UILocalNotification *notification in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
        NSString *notificationName = [notification.userInfo valueForKey:@"name"]; 
        if ([notificationName isEqualToString:name])
            return notification;
    }
    return nil;
}

Upvotes: 6

Ishu
Ishu

Reputation: 12787

use userInfo (NSDictionary type) property of UILocalNotification class to ditinguish the notifications

see this link

Upvotes: 0

Related Questions