Search code examples
ioscalendarnotificationsalarm

Display a local notification at a fixed time, ios?


I am using the below code to display the notification at a fixed time. However it just does not fit my output.

    - (IBAction)bt_set:(id)sender {

// Setting all the information about our date
int hour = 8;
int minutes = 19;
int day = 19;
int month = 10;
int year = 2014;

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];

[components setDay:day];
[components setMonth:month];
[components setYear:year];
[components setMinute:minutes];
[components setHour:hour];

NSDate *myNewDate = [calendar dateFromComponents:components];
//[components release];
//[calendar release];

[self scheduleNotificationForDate:myNewDate];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Note"
                                                message:@"Alarm has been set"
                                               delegate:self
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];
[alert show];

  }

This is the notfication code

    - (void)scheduleNotificationForDate:(NSDate *)date
  {
// Here we cancel all previously scheduled notifications
[[UIApplication sharedApplication] cancelAllLocalNotifications];

UILocalNotification *localNotification = [[UILocalNotification alloc] init];

localNotification.fireDate = date;
NSLog(@"%@",date);
NSLog(@"Notification will be shown on: %@",localNotification.fireDate);

localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = [NSString stringWithFormat:@"Your notification message"];
localNotification.alertAction = NSLocalizedString(@"View details", nil);

/* Here we set notification sound and badge on the app's icon "-1"
 means that number indicator on the badge will be decreased by one
 - so there will be no badge on the icon */
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = -1;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

}

This is the date I get in the log

    Notification will be shown on: 2014-10-19 02:49:00 +0000

I guess it has something to do with the time zone.

I have the time zone of GMT+5:30

Please help me out


Solution

  • You have some (NSDate *)date----

    Add below code to get the appropriate system time:

    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
    
    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    
    NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];