I want to fire NSLocalNotification on my custom time. Ex. 1hour before appointment time but unable to set it on my custom time.
NSDate *dt1 = [NSDate date]; //2018-07-18 10:41:22 +0000
NSString *datestr = [NSString stringWithFormat:@"%@", [NSDate date]];//2018-07-18 10:42:09 +0000
NSRange range = NSMakeRange(11,5);
NSString *cutstring = 05:00 PM;
By this I add substring to current date and change date in my custom date.
NSString *newstr = [cutstring substringWithRange:NSMakeRange(0, 5)];
NSString *changed = [datestr stringByReplacingCharactersInRange:range withString:newstr]; //2018-07-18 05:00:09 +0000
By this code I got my custom time '2018-07-18 05:00:09 +0000' on which I want to fire UILocalNotification, But issue is that when I set my custom time with NSLocalNotification it set's on such a random time like 18 July 2018 at 4:19:29 PM
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"];
NSDate *fireddate = [dateFormatter dateFromString:changed];
localNotification.fireDate = fireddate;
NSLog(@"%@",fireddate);
localNotification.alertBody = [NSString stringWithFormat:@"You have appointment in 1 hour"];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
localNotification.category = @"ACTIONABLE";
//Received Local Notification:<UIConcreteLocalNotification:
0x6000003887b0>{fire date = Wednesday, 18 July 2018 at 4:19:29 PM
India Standard Time, time zone = (null), repeat interval = 0, repeat
count = UILocalNotificationInfiniteRepeatCount, next fire date =
(null), user info = { }}
I would just create a fire date with NSDateComponents
where you can set year
, month
, hour
, minute
etc.
Something like this:
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.calendar = [NSCalendar currentCalendar];
[dateComponents setYear:year];
[dateComponents setMonth:month];
[dateComponents setDay:day];
[dateComponents setHour:hour];
NSDate *fireDate = [dateComponents date];
You can read more about it in this great article
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.