Emon
Emon

Reputation: 462

I am getting wrong notification time in iOS?

i am little bit new at iPhone application development.So at first sorry from my side if it is a lame question.Now my question is.In my app user has dial of clock where he will select hour and minute.using that hour and minute i want to set alarm for user according to alarm system and later it will set for local notification,My local notification is working only alarm time is showing wrong.So far i have done like this

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
calendar.timeZone = [NSTimeZone defaultTimeZone];
calendar.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"];

NSDateComponents *currentTimeComponents = [calendar   components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute fromDate:[NSDate date]];
currentTimeComponents.hour = [_hourlLabel.text intValue];
currentTimeComponents.minute = [_minuteLabel.text intValue];
currentTimeComponents.second = 00;
NSDate *date = [calendar dateFromComponents:currentTimeComponents];
NSLog(@"User set date: %@",date);


UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.repeatInterval = NSCalendarUnitDay;
localNotification.timeZone = [NSTimeZone defaultTimeZone];

// Schedule the notification
localNotification.fireDate = date;
localNotification.alertBody = @"It has notification from xxxx";
localNotification.alertAction = @"Show me the item";
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
localNotification.soundName = @"clockalarmfrog.mp3";

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
NSLog(@"User setup local notifications: %@",localNotification);

NSTimeInterval secs = [localNotification.fireDate timeIntervalSinceDate:[NSDate date]];
NSLog(@"animation time duration in NSTimeInterval: %f",secs);

Now suppose user has set 5:51 PM for next alarm time and current time is 5:20 PM.Now using my code i get this.

2015-05-29 17:20:11.626 xxxx[651:12248] 17:20:11 PM
2015-05-29 17:21:05.988 xxxx[651:12248] User set date: 2015-05-28 23:51:00 +0000
2015-05-29 17:21:06.099 xxxx[651:12248] User setup local notifications:  <UIConcreteLocalNotification: 0x7fd9a8c7de60>{fire date = Friday, May 29, 2015 at 5:51:00 AM Bangladesh Standard Time, time zone = Asia/Dhaka (GMT+6) offset  21600, repeat interval = NSCalendarUnitDay, repeat count =     UILocalNotificationInfiniteRepeatCount, next fire date = Saturday, May 30, 2015 at 5:51:00 AM Bangladesh Standard Time, user info = (null)}
2015-05-29 17:21:06.099 xxxx[651:12248] animation time duration in NSTimeInterval: -41406.099645
2015-05-29 17:21:06.099 xxxx[651:12248] animation reverse time duration in NSTimeInterval: 44993.900355
2015-05-29 17:21:06.168 xxxx[651:12248] duration: 44993.900355 

Upvotes: 3

Views: 1942

Answers (3)

jkr
jkr

Reputation: 660

[NSDate date] always return GMT+0 date, no matter where is your timezone.

The date you are passing to UILocalNotification has GMT+0, whereas [localNotification.timezone] set to defaultTimeZone(which is GMT +6 in your case).

// the time zone to interpret fireDate in. pass nil if fireDate is an absolute GMT time (e.g. for an egg timer).
// pass a time zone to interpret fireDate as a wall time to be adjusted automatically upon time zone changes (e.g. for an alarm clock).
@property(nonatomic,copy) NSTimeZone *timeZone;

to get correct expected output, either set localNotification.timeZone = nil; or update the date accordingly to defaultTimeZone(GMT +6) and set to fireDate.

Upvotes: 0

Vidhyanand
Vidhyanand

Reputation: 5369

You need to set current time format with AM/PM to Local Notification fireDate property in order to fire alarm at current time.

Below method is used to convert your Hours, Minutes and Period(AM/PM) to current date format..so that it fires the alarm with specified time.

//Add this method

- (NSDate *)dateModifiedWithHours:(NSString *)hours
                           minutes:(NSString *)minutes
                         andPeriod:(NSString *)period
{
    NSDate *dateModified = NSDate.date;

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSMinuteCalendarUnit fromDate:dateModified];

    [components setMinute:minutes.intValue];

    int hour = 0;

    if ([period.uppercaseString isEqualToString:@"AM"]) {

        if (hours.intValue == 12) {
            hour = 0;
        }
        else {
            hour = hours.intValue;
        }
    }
    else if ([period.uppercaseString isEqualToString:@"PM"]) {

        if (hours.intValue != 12) {
            hour = hours.intValue + 12;
        }
        else {
            hour = 12;
        }
    }
    [components setHour:hour];

    dateModified = [calendar dateFromComponents:components];

    return dateModified;
} 

Modify your code as below

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
calendar.timeZone = [NSTimeZone defaultTimeZone];
calendar.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"];

NSDateComponents *currentTimeComponents = [calendar   components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute fromDate:[NSDate date]];
currentTimeComponents.hour = [_hourlLabel.text intValue];
currentTimeComponents.minute = [_minuteLabel.text intValue];
currentTimeComponents.second = 00;
NSDate *date = [calendar dateFromComponents:currentTimeComponents];
NSLog(@"User set date: %@",date);


UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.repeatInterval = NSCalendarUnitDay;
localNotification.timeZone = [NSTimeZone defaultTimeZone];


NSDate *firedModifiedDate = [self dateModifiedWithHours:@"5"
                                               minutes:@"51"
                                             andPeriod:@"pm"];

// Schedule the notification
localNotification.fireDate = firedModifiedDate;
localNotification.alertBody = @"It has notification from xxxx";
localNotification.alertAction = @"Show me the item";
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
localNotification.soundName = @"clockalarmfrog.mp3";

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
NSLog(@"User setup local notifications: %@",localNotification);

NSTimeInterval secs = [localNotification.fireDate timeIntervalSinceDate:[NSDate date]];
NSLog(@"animation time duration in NSTimeInterval: %f",secs);

Then your Log looks like below

2015-05-29 17:21:06.099 xxxx[651:12248] User setup local notifications:  <UIConcreteLocalNotification: 0x7fd9a8c7de60>{fire date = Friday, May 29, 2015 at 5:51:00 PM Bangladesh Standard Time, time zone = Asia/Dhaka (GMT+6) offset  21600, repeat interval = NSCalendarUnitDay, repeat count =     UILocalNotificationInfiniteRepeatCount, next fire date = Saturday, May 30, 2015 at 5:51:00 AM Bangladesh Standard Time, user info = (null)}

Hope it helps you..to fix.

Upvotes: 1

Shruti
Shruti

Reputation: 1849

Change

localNotification.timeZone = [NSTimeZone defaultTimeZone];

to

localNotification.timeZone = [NSTimeZone localTimeZone];

Upvotes: 1

Related Questions