views:

42

answers:

0

HI I m creating a UILocalNotification for IPhone for repeated Interval for everyday.

-(void)createNotification

{

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
   if (localNotif == nil)
{
    NSLog(@"createNotification IF");
    return;
}
NSLog(@"createNotification");
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];    
//Get the current date
NSDate *todayDate = [NSDate date];  
//Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit ) 
                                               fromDate:todayDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) 
                                               fromDate:todayDate];

//Set up the fire time for notification.



 [dateComps setHour:[timeComponents hour];
 [dateComps setMinute:[timeComponents minute];  
 [dateComps setSecond:0];
  NSDate *bdayDate = [calendar dateFromComponents:dateComps];



localNotif.fireDate = bdayDate;
NSLog(@"NotificationDate :%@",bdayDate);
localNotif.timeZone = [NSTimeZone defaultTimeZone];

//Set repaet Time(everyday).
localNotif.repeatInterval=NSDayCalendarUnit;
localNotif.alertBody = @"you have got a new notification";
//Set the action button
localNotif.alertAction = @"View";
//set alarm sound.
localNotif.soundName = @"alarm.wav";
localNotif.applicationIconBadgeNumber = 1;

// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];   
[localNotif release];

}

-(void)showNotification {

birthdayPersonsInfo=[[NSMutableArray alloc]init];    
count=0;        
//Create reference to read/write from AddressBook. 
ABAddressBookRef addressBook=ABAddressBookCreate();

//To get all the contacts from AddressBook.
CFArrayRef allPeople=ABAddressBookCopyArrayOfAllPeople(addressBook);

//Accessing all the records from AddressBook to 
//find birthday today.
CFIndex npeople=ABAddressBookGetPersonCount(addressBook);
for(int i=0;i<npeople;i++)
{
    ABRecordRef ref=CFArrayGetValueAtIndex(allPeople, i);
    CFDateRef dob=ABRecordCopyValue(ref, kABPersonBirthdayProperty);
    [self findBirthDayToday:(NSDate *)dob withRef:ref];

}      //End Of For

//if there is any Birthday today.
if(count>0)
{

    //Adding UITableView.
    birthdayTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];
    self.birthdayTableView.backgroundView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Background.png"]];


    birthdayTableView.dataSource=self;
    [self.view addSubview:birthdayTableView];
    [birthdayTableView release];


}        
//there is no Birthday today.
else 
{

    while ([[self.view subviews] count]>0) {
        [[[self.view subviews]objectAtIndex:0]removeFromSuperview];
    }

    lblNoBirthday=[[UILabel alloc]initWithFrame:CGRectMake(27, 60, 290, 20)];
    [lblNoBirthday setText:@"The Birthday list is empty for today."];
    [self.view addSubview:lblNoBirthday];                
    [lblNoBirthday release];
}

}
this is working for me it creates a Local notification everyday.
But i want that when count=0 (No birthday in that day) no notification should be generate in that day.
It should generate notification only for count>0.(have atleast one birthday on that day).
Plz update/edit my code for this
. i have gone through -
http://stackoverflow.com/questions/3379088/how-to-cancel-a-local-notification-in-iphone
it cancel the Notification for count=0 but does't recreate when count>0.
please help me for above.
lots lots thanks for ant help.