ios - UILocalNotification wrong fire date -
i have alarm run mon, sat & sun. pretty simple stuff. use code create uilocalnotification. since today sat testing see if alarm goes off today.
//user picks time date picker shows hours , mins nsdate *selecteddate = [datepicker date]; nscalendar *calendar = [nscalendar currentcalendar]; nsdatecomponents *selecdatecomp = [calendar components:(nshourcalendarunit | nsminutecalendarunit) fromdate:selecteddate]; nsinteger hour = [selecdatecomp hour]; nsinteger minute = [selecdatecomp minute]; //nslog(@"hour: %d ... minute: %d ...", hour, minute); // set components fire time nsdate *now = [nsdate date]; nsdatecomponents *componentsforfiredate = [calendar components:nsyearcalendarunit|nsmonthcalendarunit|nsweekcalendarunit|nsweekdaycalendarunit|nshourcalendarunit|nsminutecalendarunit fromdate:now]; [componentsforfiredate sethour:hour]; [componentsforfiredate setminute:minute] ; //testing [componentsforfiredate setweekday:2]; //mon [componentsforfiredate setweekday:1]; //sun [componentsforfiredate setweekday:7]; //sat nsdate *firedateofnotification = [calendar datefromcomponents: componentsforfiredate]; nslog(@"componentsforfiredate: %@ ... firedateofnotification: %@", componentsforfiredate, firedateofnotification); //i use firedateofnotification date in uilocalnotification uilocalnotification *notification = [[uilocalnotification alloc] init] ; notification.firedate = firedateofnotification ; notification.timezone = [nstimezone localtimezone] ; notification.alertbody = [nsstring stringwithformat: @"wake time!"] ; notification.userinfo= [nsdictionary dictionarywithobject:titlestr forkey:titlestr]; notification.repeatinterval = nsweekcalendarunit; nsmutablestring *notifsoundstr = [nsmutablestring stringwithformat:@"%@ small.mp3", alarmsound]; notification.soundname=notifsoundstr; [[uiapplication sharedapplication] schedulelocalnotification:notification] ;
here's problem, if @ firedate last week. because of no notification run today though today sat.
componentsforfiredate: <nsdatecomponents: 0xc0f9080> calendar year: 2014 month: 5 leap month: no hour: 11 minute: 16 week (obsolete): 22 weekday: 8 ... firedateofnotification: 2014-05-25 15:16:00 +0000
now here's crazy part, if remove sun notification , sat & mon, fire time works , notification happens today. what's happening here? lost
... [componentsforfiredate setweekday:2]; //mon [componentsforfiredate setweekday:7]; //sat componentsforfiredate: <nsdatecomponents: 0xf8af890> calendar year: 2014 month: 5 leap month: no hour: 11 minute: 19 week (obsolete): 22 weekday: 7 ... firedateofnotification: 2014-05-31 15:19:00 +0000
a couple of things doing this:
[componentsforfiredate setweekday:2]; //mon [componentsforfiredate setweekday:1]; //sun [componentsforfiredate setweekday:7]; //sat
this not doing think it's doing. not setting 3 weekdays setting single value , changing twice. last thing set value gets used.
to set alarm 3 different days need create 3 different notifications.
to honest fact data in past has no bearing on problem because have set notification.repeatinterval = nsweekcalendarunit;
therefore ignore first date , schedule next week.
for example if log out datecomponents get
<nsdatecomponents: 0x8d89c80> calendar year: 2014 month: 5 leap month: no hour: 16 minute: 41 week (obsolete): 22 weekday: 7
but if print local notification shows correct date - week on
<uiconcretelocalnotification: 0x8f49580>{fire date = saturday, may 31, 2014 @ 4:41:00 pm british summer time, time zone = europe/london (gmt+1) offset 3600 (daylight), repeat interval = nsweekcalendarunit, repeat count = uilocalnotificationinfiniterepeatcount, next fire date = saturday, june 7, 2014 @ 4:41:00 pm british summer time, user info = (null)}
the format using components deprecated ns<interval>calendarunit
, should instead use nscalendarunit<interval>
better form it's last part of identifier changes. allows autocomplete work type
Comments
Post a Comment