简体   繁体   中英

Local notification set default time 9:00 am for user timezone in iOS

I want set notification default 9:00 am for every user timezone time.

How to create fire date for default time 9:00 am .

Hello I got it this Ans:-

NSDate *pickerDate = [NSDate date]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:DT_FORMATE_BIRTHDATE];
    NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:DT_TIME_ZONE_GMT];
    [dateFormatter setTimeZone:gmt];

    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
    NSDateComponents *timeComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
                                                   fromDate:pickerDate];
    [timeComponents setHour:9];
    [timeComponents setMinute:00];
    [timeComponents setSecond:0];

    NSDate *dtFinal = [calendar dateFromComponents:timeComponents];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];

    NSString *fierDate = [formatter stringFromDate:dtFinal];
  1. This is a full tutorial regarding Local Notification

  2. Below is Solve issue of timezone which mention above. Please review these timezone function in details and set appropriate timezone in your function.


[NSTimeZone defaultTimeZone]

[NSTimeZone systemTimeZone]

[NSTimeZone localTimeZone]

NSTimeZone: what is the difference between localTimeZone and systemTimeZone?

Try this code.

 NSDate *pickerDate = [date_picker date];
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
localNotification.alertBody = @"Alert Body Message";
localNotification.alertAction = @"Alert Action Message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM