ios - local notification are sent at wrong time -
i want sent 2 local notifications daily @ particular time in ios,
but notifications sent @ wrong time , sends multiple times in day , not once,
here code snippet,
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { // running on ios8? if ([application respondstoselector:@selector(registerusernotificationsettings:)]) { uiusernotificationsettings *settings = [uiusernotificationsettings settingsfortypes:(uiusernotificationtypebadge|uiusernotificationtypealert|uiusernotificationtypesound) categories:nil]; [application registerusernotificationsettings:settings]; } else // ios 7 or earlier { uiremotenotificationtype mytypes = uiremotenotificationtypebadge | uiremotenotificationtypealert | uiremotenotificationtypesound; [application registerforremotenotificationtypes:mytypes]; } application.applicationiconbadgenumber = 0; //this if called once.... if ([prefs stringforkey:@"notification"] == nil) { //... 1st notification ... nsdate *now = [nsdate date]; nscalendar *calendar = [[nscalendar alloc] initwithcalendaridentifier:nsgregoriancalendar]; nsdatecomponents *components = [calendar components:nsyearcalendarunit|nsmonthcalendarunit|nsdaycalendarunit fromdate:now]; [components sethour:11]; [components setminute:24]; // gives today's date @ 11am nsdate *next11am = [calendar datefromcomponents:components]; if ([next11am timeintervalsincenow] < 0) { // if today's 9am occurred, add 24hours tomorrow's next11am = [next11am datebyaddingtimeinterval:60*60*24]; } uilocalnotification *notification = [[uilocalnotification alloc] init]; notification.firedate = next11am; notification.alertbody = @"notification 1."; // set repeat interval daily notification.repeatinterval = nsdaycalendarunit; [[uiapplication sharedapplication] schedulelocalnotification:notification]; //... 2nd notification ... nsdate *now1 = [nsdate date]; nscalendar *calendar1 = [[nscalendar alloc] initwithcalendaridentifier:nsgregoriancalendar]; nsdatecomponents *components1 = [calendar1 components:nsyearcalendarunit|nsmonthcalendarunit|nsdaycalendarunit fromdate:now1]; [components1 sethour:18]; [components1 setminute:30]; // gives today's date @ 9am nsdate *next6pm = [calendar datefromcomponents:components]; if ([next6pm timeintervalsincenow] < 0) { // if today's 6pm occurred, add 24hours tomorrow's next6pm = [next6pm datebyaddingtimeinterval:60*60*24]; } uilocalnotification *notification1 = [[uilocalnotification alloc] init]; notification1.firedate = next6pm; notification1.alertbody = @"notification 2."; // set repeat interval daily notification1.repeatinterval = nsdaycalendarunit; [[uiapplication sharedapplication] schedulelocalnotification:notification1]; } }
where doing mistake ? please help
thanks in advance!!
you check [prefs stringforkey:@"notification"]
, haven't defined prefs
nil, , never appear set value notification
missing. of these things mean you're adding when don't expect to.
you're calling schedulelocalnotification:
calling cancellocalnotification:
or cancelalllocalnotifications
so, depending on how user uses app, or how you're doing testing, can adding multiple notifications @ same time. use scheduledlocalnotifications
check scheduled.
you should using currentcalendar
calendar need respect users settings.
Comments
Post a Comment