问题是我的通知在设置时没有通过。我在笔尖放了一个日期选择器,在它下面放了一个按钮。用户应该在选择器中设置日期,然后单击按钮将通知设置为在日期之前60小时触发。所以,看起来我只是遇到让被解雇者识别日期选择器中的日期的问题。这是代码,它在我的视图控制器中:
- (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天了,并且无法弄清楚为什么我的通知在他们应该的时候没有通过。非常感谢任何帮助,谢谢!
答案 0 :(得分:3)
您是否了解UILocalNotifications和远程通知是不同的东西?
在didRegisterForRemoteNotificationsWithDeviceToken中,您将获得用于远程通知的设备令牌。远程通知从服务器发送,因此您必须将该令牌保存在服务器上,然后使用服务器发送任何远程通知。
你为什么不改变这个:
localNotif.fireDate = [eventDate dateByAddingTimeInterval:-630*60];
到此:
localNotif.fireDate = [[NSDate date] dateByAddingTimeInterval:60];
然后本地通知会在一分钟后发生。也许问题是你正在设置的日期。
答案 1 :(得分:1)
您如何知道自己的通知无效?
本地和推送通知不同。如果您的应用当前处于有效状态,则本地通知实际上无法显示提醒消息。它只会调用UIApplicationDelegateMethod
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification