ios - Local Notifications are working in the simulator, but doesn't work when i test it on my actual device -
i have timer function calls local notification timer ever set amount of seconds , supposed run until stopped button.
everything in viewcontroller.m file.
here code:
-(void)timer{ //num slider value num1 = num*60.0; self.showertimer = [nstimer scheduledtimerwithtimeinterval:num1 target:self selector:@selector(notification) userinfo:nil repeats:yes]; }
my notification should go off.
-(void)notification{ //nsdate *alarmtime = [[nsdate date] datebyaddingtimeinterval:num]; uiapplication *app = [uiapplication sharedapplication]; uilocalnotification *notifyalarm = [[uilocalnotification alloc] init]; // current date/time nsdate *today = [nsdate date]; nsdateformatter *dateformatter = [[nsdateformatter alloc] init]; // display in 12hr/24hr (i.e. 11:25pm or 23:25) format according user settings [dateformatter settimestyle:nsdateformattershortstyle]; nsstring *currenttime = [dateformatter stringfromdate:today]; //nslog(@"user's current time in preference format:%@",currenttime); notifyalarm.firedate = [nsdate datewithtimeintervalsincenow:0]; notifyalarm.timezone = [nstimezone defaulttimezone]; notifyalarm.repeatinterval = 0; notifyalarm.soundname = @""; notifyalarm.alertbody = [nsstring stringwithformat:@"notified @ %@",currenttime]; [app schedulelocalnotification:notifyalarm]; }
my first advise set timer on main loop.
without property:
dispatch_async(dispatch_get_main_queue(), ^{ // enclose timer code here [nstimer scheduledtimerwithtimeinterval:num1 target:self selector:@selector(notification) userinfo:nil repeats:yes]; });
if want keep property:
__weak myviewcontroller *ablockself = self; dispatch_async(dispatch_get_main_queue(), ^{ // enclose timer code here ablockself.showtimer = [nstimer scheduledtimerwithtimeinterval:num1 target:self selector:@selector(notification) userinfo:nil repeats:yes]; });
second, not need keep reference of timer in --> self.showertimer
. because run loop keeps strong reference repeating timer.
third, variable naming convention off. showertimer
should named showertimer
. although nothing problem.
finally, code looks fine , if notification working on simulator, check do not disturb setting on device. should off local notification show up.
Comments
Post a Comment