0

This might not be a difficult question to answer for many of you,but would you let me know why the notification is fired, even though i delete the event for it from DataBase(Core-Data).

Thank You,

Best Regards.

  • Do you call cancelLocalNotification when you delete your event? http://stackoverflow.com/questions/6340664/delete-a-particular-local-notification – Valent Richie May 31 '13 at 05:22
  • Nope i did not use it.I believe it is helpful in the case where you want to remove the UILocalNotification when the event is fired.Probably IM not too sure. @verbumdei –  May 31 '13 at 05:27
  • When i click a button that Clears the DB, Notifications still tend to pop even the event is no longer available in DB. @verbumdei –  May 31 '13 at 05:28
  • It is because core data records are not in any way linked to the local notification unless you link them together. When you schedule a local notification, you can assign an identifier in the user info. When you remove an event, you need to cancel the local notification with the same identifier. – Valent Richie May 31 '13 at 05:32
  • That sounds pretty useful @verbumdei , but it would be great help if you give me a sample for doing so. I have written this on click of SAVE button that saves data to DB –  May 31 '13 at 05:36
  • `UILocalNotification *lc=[[UILocalNotification alloc] init]; lc.fireDate= objDate; lc.alertBody=@"Hey Sam it's time for some work"; lc.timeZone=[NSTimeZone defaultTimeZone]; lc.soundName = UILocalNotificationDefaultSoundName; lc.alertAction=@"OK"; [[UIApplication sharedApplication] scheduleLocalNotification:lc]; ` –  May 31 '13 at 05:37

3 Answers3

1

You have to cancelLocalNotification to stop firing the UILocalNotification

EDIT

I added here a method to cancel local notification. You need to maintain an id in the notification.userInfo(dictionary) for every notifications. if you want to cancel notification pass the id of notification in this method

-(void)cancelNotificationForId:(NSString*)id {

    NSArray *notifs = [[UIApplication sharedApplication]scheduledLocalNotifications];
    for (UILocalNotification *notification in notifs) {
        if ([[notification.userInfo objectForKey:@"alarm_id"] isEqualToString:id]) {
            [[UIApplication sharedApplication] cancelLocalNotification:notification];
        }
    }

} 

Try this method

manujmv
  • 6,450
  • 1
  • 22
  • 35
  • Nope i did not use it.I believe it is helpful in the case where you want to remove the UILocalNotification when the event is fired.Probably IM not too sure. When i click a button that Clears the DB, Notifications still tend to pop even the event is no longer available in DB. @manujmv –  May 31 '13 at 05:30
  • @MohammedNaveedShaikh: as local notifications are not bound to your db but to iPhone OS, only removing them from your DB will not work. You must cancel them. – Yogi May 31 '13 at 05:33
  • @MohammedNaveedShaikh: No. u r mistaken the purpose of cancelLocalNotification. CancelLocalNotification is used to stop scheduling – manujmv May 31 '13 at 05:44
  • @manujmv : Okay !! thank You for that.Hey this is the hierarchy i have 3fields and i have a save button that triggers notification to fire based on the time set.But when i clear the DB the notification still fires even thou there is nothing in the DB.I Hope you get it.If not share your e-mail add i will mail you the code. –  May 31 '13 at 05:45
0

You need to remove notification with the help of notification ID.

iPhone developer.
  • 2,122
  • 1
  • 16
  • 17
  • Please give an example or sample todo so @user2339310. –  May 31 '13 at 05:31
  • NSString *myIDToCancel = @"some_id_to_cancel"; UILocalNotification *notificationToCancel=nil; for(UILocalNotification *aNotif in [[UIApplication sharedApplication] scheduledLocalNotifications]) { if([[aNotif.userInfo objectForKey:@"ID"] isEqualToString:myIDToCancel]) { notificationToCancel=aNotif; break; } } [[UIApplication sharedApplication] cancelLocalNotification:notificationToCancel]; – iPhone developer. May 31 '13 at 05:33
  • :Buddy where MI suppose to write this on the click of save button that saves the data or in some delegate method. `UILocalNotification *lc=[[UILocalNotification alloc] init]; lc.fireDate= objDate; lc.alertBody=@"Hey Sam it's time for some work"; lc.timeZone=[NSTimeZone defaultTimeZone]; lc.soundName = UILocalNotificationDefaultSoundName; lc.alertAction=@"OK"; [[UIApplication sharedApplication] scheduleLocalNotification:lc]; ` –  May 31 '13 at 05:40
  • From where you want to remove notification. – iPhone developer. May 31 '13 at 05:41
  • I dont want the notification to fire when the event is deleted from DB.hierarchy is : i have 3fields and i have a save button that triggers notification to fire based on the time set.But when i clear the DB the notification still fires even thou there is nothing in the DB.I Hope you get it.If not share your e-mail add i will mail you the code. –  May 31 '13 at 05:44
  • So, when you add event to database add notification with ID. When you delete event from database, in same function you can delete that notification with the help of Id which you get from database. – iPhone developer. May 31 '13 at 05:46
  • KKK i'l try doing that.Will get to you once its done.ThanK you. –  May 31 '13 at 05:51
0

For more reference regarding the terms mentioned in answers by user2339310 and manujmv, please refer Apple Documentation for Canceling Local Notification. This will clear the concept of UILocalNotification and there cancellation.

Yogi
  • 3,578
  • 3
  • 35
  • 56