当我转移程序时,本地通知将出现在模拟器中,但不会出现在我的掌上电脑设备中。
这是我的“操作”按钮,它将启动计时器,并且根据滑块的值,每隔几分钟就会出现一个通知。
- (IBAction)StartTimer:(id)sender {
checker = 1;
[self pressed];
NSString *title = @"Shower Alarm";
NSString *message = @"Your alarm will start now";
NSString *okAlert = @"Ok";
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okButton = [UIAlertAction actionWithTitle:okAlert style:UIAlertActionStyleCancel handler:nil];
[alert addAction:okButton];
[self presentViewController:alert animated:YES completion:nil];
check.text = [NSString stringWithFormat: @"Showering In Progress..."];
//[self Timer];
dispatch_async(dispatch_get_main_queue(), ^{
num1 = num*60.0;
_ShowerTimer = [NSTimer scheduledTimerWithTimeInterval:num1
target:self
selector:@selector(notification)
userInfo:nil
repeats:YES];});}
这是我的计时器@selector
调用的Notification方法-(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:1.0];
notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
notifyAlarm.repeatInterval = 0;
notifyAlarm.soundName = @"";
notifyAlarm.alertBody = [NSString stringWithFormat:@"Notified at %@",currentTime];
[app scheduleLocalNotification:notifyAlarm];}
我也在我的didFinishLaunching方法中请求了许可。我读了一些关于didReceiveLocalNotification的内容,但这会影响我的设备而不是模拟器?我可以锁定我的模拟器,我仍然会收到通知,但我的设备不会收到任何通知。
答案 0 :(得分:2)
不要使用计时器,而是打电话
[self notification]
并修改
notifyAlarm.fireDate = [NSDate dateWithTimeIntervalSinceNow:1.0];
这样有60秒
notifyAlarm.fireDate = [NSDate dateWithTimeIntervalSinceNow:60.0];
。在手机上将应用程序放在后台时可能会很接近。即使应用程序在后台关闭或冻结,也可以调度计时器。那有用吗?