Sunday, 25 March 2012

How to Add Local Notification in IPhone App

 UILocalNotification *localNotif;

   //==================== Add Local Notification ===========================
    localNotif = [[UILocalNotification alloc] init];
    localNotif.fireDate = dateStartDate;
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
    localNotif.alertBody = txtName.text;
    localNotif.alertAction = @"View";
    localNotif.soundName = UILocalNotificationDefaultSoundName;
   
    // Specify custom data for the notification
    infoDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:txtName.text,@"name",nil];
    localNotif.userInfo = infoDict;
   
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
   
    [localNotif release];
   
    //=========================================================


   //============  Delete Previous Notification ==================
       
        UILocalNotification *notificationToCancel=nil;
        for(UILocalNotification *aNotif in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
            if( [[aNotif.userInfo objectForKey:@"name"] isEqualToString:[editTask objectForKey:@"name"]]) {
                notificationToCancel=aNotif;
                break;
            }
        }
        if(notificationToCancel)
            [[UIApplication sharedApplication] cancelLocalNotification:notificationToCancel];
       
        //==================================
       

1 comment: