iOS:本地通知未按时触发
问题是我的通知在设置时没有通过。我在笔尖中放置了一个日期选择器,并在其下方放置了一个按钮。用户应该在选择器中设置日期,然后单击按钮将通知设置为在该日期之前 60 小时触发。所以,看起来我只是在让 fireate 识别日期选择器中的日期时遇到问题。这是代码,它在我的视图控制器中:
- (IBAction)scheduleNotifButton:(id)sender {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
NSDate *selectedDate = [self.datePicker date];
localNotif.fireDate = [selectedDate dateByAddingTimeInterval:-60*60*60];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = @"Event is in three days!";
localNotif.alertAction = nil;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Event scheduled."
message:@"You will be notified three days before the event."
delegate:nil
cancelButtonTitle:@"Okay."
otherButtonTitles:nil];
[alert show];
}
如果有帮助的话,这是一些附加代码,这是我的视图控制器中的更多代码,用于将日期保存在用户输入的选择器中:
- (IBAction)dateChanged:(id)sender
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDate *selectedDate = [self.datePicker date];
[defaults setObject:selectedDate forKey:@"ImportantDatesViewController.selectedDate"];
[defaults synchronize];
}
- (void)viewDidLoad {
NSDate *storedDate = [[NSUserDefaults standardUserDefaults]
objectForKey:@"ImportantDatesViewController.selectedDate"];
if (storedDate == nil) {
storedDate = [NSDate date];
}
[self.datePicker setDate:storedDate animated:NO];
}
我一直在挖掘这段代码已经使用了 3 天,但无法弄清楚为什么我的通知在应该的时候没有通过。非常感谢任何帮助,谢谢!
The issue is that my notifications aren't going through when they are set up to. I put a date picker in the nib and a button underneath it. The user is supposed to set the date in the picker, and clicking the button set up the notification to fire 60 hours before the date. So, it looks like I'm just having issues with getting the firedate to recognize the date in the date picker. Here is the code, it is in my view controller:
- (IBAction)scheduleNotifButton:(id)sender {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
NSDate *selectedDate = [self.datePicker date];
localNotif.fireDate = [selectedDate dateByAddingTimeInterval:-60*60*60];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = @"Event is in three days!";
localNotif.alertAction = nil;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Event scheduled."
message:@"You will be notified three days before the event."
delegate:nil
cancelButtonTitle:@"Okay."
otherButtonTitles:nil];
[alert show];
}
And here is some additional code if that helps, this is more code from my view controller that deals with saving the date in the picker that the user entered:
- (IBAction)dateChanged:(id)sender
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDate *selectedDate = [self.datePicker date];
[defaults setObject:selectedDate forKey:@"ImportantDatesViewController.selectedDate"];
[defaults synchronize];
}
- (void)viewDidLoad {
NSDate *storedDate = [[NSUserDefaults standardUserDefaults]
objectForKey:@"ImportantDatesViewController.selectedDate"];
if (storedDate == nil) {
storedDate = [NSDate date];
}
[self.datePicker setDate:storedDate animated:NO];
}
I've been digging through this code for 3 days now and cannot figure out why my notifications aren't going through when they are supposed to. Any help is very much appreciated, thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你知道 UILocalNotifications 和远程通知是不同的东西吗?
在 didRegisterForRemoteNotificationsWithDeviceToken 中,您可以获得用于远程通知的设备令牌。远程通知是从服务器发送的,因此您必须将该令牌保存在服务器上,然后使用您的服务器发送任何远程通知。
为什么不将这个: 更改
为:
那么本地通知应该会在一分钟内关闭。也许问题出在您设置的日期上。
Do you understand that UILocalNotifications and remote notifications are different things?
In didRegisterForRemoteNotificationsWithDeviceToken you get the device token to be used for remote notifications. Remote notifications are sent from a server so you have to save that token on your server and then use your server to send any remote notifications.
Why don't you change this:
to this:
Then the local notification should go off in a minute. Maybe the problem is the date that you are setting.
您怎么知道您的通知不起作用?
本地通知和推送通知是不同的。如果您的应用程序当前处于活动状态,则本地通知实际上不会显示警报消息。它只会调用 UIApplicationDelegateMethod
How do you know your notifications aren't working?
Local and push notifications are different. If your app is currently active, a local notification won't actually show an alert message. It will just call a UIApplicationDelegateMethod