Ios 本地通知在模拟器中工作,但不';当我在我的实际设备上测试它时,它无法工作
我有一个计时器,它的功能是调用本地通知计时器,每次设置秒数,它应该运行,直到被按钮停止 所有内容都在我的viewcontroller.m文件中 这是我的密码:Ios 本地通知在模拟器中工作,但不';当我在我的实际设备上测试它时,它无法工作,ios,objective-c,iphone,notifications,local,Ios,Objective C,Iphone,Notifications,Local,我有一个计时器,它的功能是调用本地通知计时器,每次设置秒数,它应该运行,直到被按钮停止 所有内容都在我的viewcontroller.m文件中 这是我的密码: -(void)Timer{ //num is a slider value num1 = num*60.0; self.ShowerTimer = [NSTimer scheduledTimerWithTimeInterval:num1 target:self
-(void)Timer{
//num is a slider value
num1 = num*60.0;
self.ShowerTimer = [NSTimer scheduledTimerWithTimeInterval:num1
target:self
selector:@selector(notification)
userInfo:nil
repeats:YES];
}
我应该发出的通知
-(void)notification{
//NSDate *alarmTime = [[NSDate date] dateByAddingTimeInterval:num];
UIApplication *app = [UIApplication sharedApplication];
UILocalNotification *notifyAlarm = [[UILocalNotification alloc] init];
// get 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 to User Settings
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *currentTime = [dateFormatter stringFromDate:today];
//NSLog(@"User's current time in their preference format:%@",currentTime);
notifyAlarm.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];
notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
notifyAlarm.repeatInterval = 0;
notifyAlarm.soundName = @"";
notifyAlarm.alertBody = [NSString stringWithFormat:@"Notified at %@",currentTime];
[app scheduleLocalNotification:notifyAlarm];
}
我的第一个建议是在主循环上设置计时器 无财产:
dispatch_async(dispatch_get_main_queue(), ^{
// Enclose your timer code here
[NSTimer scheduledTimerWithTimeInterval:num1 target:self selector:@selector(notification) userInfo:nil repeats:YES];
});
__weak MyViewController *aBlockSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
// Enclose your timer code here
aBlockSelf.showTimer = [NSTimer scheduledTimerWithTimeInterval:num1 target:self selector:@selector(notification) userInfo:nil repeats:YES];
});
如果您想保留您的财产:
dispatch_async(dispatch_get_main_queue(), ^{
// Enclose your timer code here
[NSTimer scheduledTimerWithTimeInterval:num1 target:self selector:@selector(notification) userInfo:nil repeats:YES];
});
__weak MyViewController *aBlockSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
// Enclose your timer code here
aBlockSelf.showTimer = [NSTimer scheduledTimerWithTimeInterval:num1 target:self selector:@selector(notification) userInfo:nil repeats:YES];
});
第二,您不需要像下面这样保留计时器的引用-->self.bathertimer
。这是因为运行循环保持了对重复计时器的强引用
第三,您的变量命名约定已关闭<代码>淋浴定时器应命名为淋浴定时器
。虽然这与你的问题无关
最后,您的代码看起来很好,如果通知在模拟器上工作,请检查设备上的请勿干扰设置。本地通知显示时应该关闭。您是否为本地通知编写了接收代码?请提供您为本地通知所做的所有代码您是否确保在任何情况下都不会禁用推送通知?对于本地推送,您现在(iOS8和更高版本)也需要用户的接受。请看一下:设置->你的手机上的通知device@SahebRoy接收代码是什么?@Lepidopteron是的,我在AppDelegated的launchedwithoptions中有它我会试试它dispatch_async到底做什么?只是让它优先?我将如何从调度调用我的计时器功能?在我的开始按钮操作中,我调用[自拍];要启动计时器,我如何将其从调度中拉出?在模拟器中仍然有效,但在我将其传输到手机时无效。您是否检查了设备上的“请勿打扰”设置?