Tuesday 9 October 2012

Local Notification in iPhone SDK.


Change some code and get your output...
here i give the logic for set notification before 1 day of selected day.

-(IBAction)btnGo_Clicked:(id)sender{

    NSLog(@"\n ----------->>Go Clicked");
    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls != nil) {
        NSString *kRemindMeNotificationDataKey = @"kRemindMeNotificationDataKey";

        ///bellow i set 1 day before give us notification
        NSDateComponents *dc = [[NSCalendar currentCalendar] components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSQuarterCalendarUnit fromDate:[exdatePicker date]];
        [dc setDay:dc.day - 1];
        NSDate *noticeDate = [[NSCalendar currentCalendar] dateFromComponents:dc];

        
        NSDateFormatter* dateFormatterstring = [[NSDateFormatter alloc] init];
        [dateFormatterstring setDateFormat:@"dd-MM-yyyy hh:mm:ss a"];
        NSString *dateString = [dateFormatterstring stringFromDate:noticeDate];

        txtExpirayDate.text = dateString;
        UILocalNotification *notification = [[cls alloc] init];

        notification.fireDate = noticeDate;
        notification.timeZone = [NSTimeZone defaultTimeZone];
        notification.alertBody = [NSString stringWithFormat:@"%@ your Licence Expiry Date is Tommorow",txtLicence.text];
        notification.alertAction = @"Show me";
        notification.soundName = UILocalNotificationDefaultSoundName;
        notification.applicationIconBadgeNumber = 1;
        NSDictionary *userDict = [NSDictionary dictionaryWithObject:txtLicence.text forKey:kRemindMeNotificationDataKey];
        notification.userInfo = userDict;
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
        [notification release];
    }
    exdatePicker.hidden=YES;
}

No comments:

Post a Comment