how to identify a particular notification in ios sdk

lara1435 picture lara1435 · Oct 7, 2011 · Viewed 7.3k times · Source

actually am developing an alarm project, now i have a doubt on Local notification. how can i identify a particular notification. we can't even set tag to local notification then how can i differentiate them.

example:

notification:1

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = selectedDate; 
    localNotification.alertBody = @"you got work";
    localNotification.alertAction = @"Snooze";
    localNotification.repeatInterval = NSDayCalendarUnit;
    localNotification.soundName = UILocalNotificationDefaultSoundName;

    NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"setNotificationForEveryDay", @"key", nil];
    localNotification.userInfo = infoDict;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    [localNotification release];

notification:2,

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = another selectedDate; 
    localNotification.alertBody = @"i got work";
    localNotification.alertAction = @"Snooze";
    localNotification.repeatInterval = NSDayCalendarUnit;
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"setNotificationForEveryDay", @"key", nil];
    localNotification.userInfo = infoDict;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    [localNotification release];

now i'm in situation to delete the second notification how can i do it... please help me.. thanks in advance..

Answer

Radix picture Radix · Oct 21, 2011

My guess is that use the userInfo for distinguishing the local notifications that would be a better idea but for that you need to set the userInfo of the local notification.

Like you could do something like this

 if ([Your_notification_Object.userInfo valueForKey:@"Key 1"]==@"Object 1") {

            NSLog(@"This is notification 1");
        }

now for your second requirement i.e for the deleting part do you want to delete the notification when it is identified as n1 or n2 then in that case you could modify the above code and add this

if ([Your_notification_Object.userInfo valueForKey:@"Key 1"]==@"Object 1") {

            NSLog(@"This is notification 1");
[[UIApplication sharedApplication] cancelLocalNotification:Your_notification_Object];


        }

Place the above code as per your convenience