Iphone iOS:本地通知不';不要准时开火

Iphone iOS:本地通知不';不要准时开火,iphone,ios,ipad,notifications,local,Iphone,Ios,Ipad,Notifications,Local,问题是我的通知在设置为时无法通过。我在笔尖上放了一个日期选择器,下面有一个按钮。用户应该在选择器中设置日期,然后单击按钮设置在日期前60小时发出的通知。所以,看起来我只是在让firedate识别日期选择器中的日期方面遇到了问题。以下是代码,它位于我的视图控制器中: - (IBAction)scheduleNotifButton:(id)sender { UILocalNotification *localNotif = [[UILocalNotification alloc] init]; N

问题是我的通知在设置为时无法通过。我在笔尖上放了一个日期选择器,下面有一个按钮。用户应该在选择器中设置日期,然后单击按钮设置在日期前60小时发出的通知。所以,看起来我只是在让firedate识别日期选择器中的日期方面遇到了问题。以下是代码,它位于我的视图控制器中:

- (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];

}
下面是一些额外的代码,如果有帮助的话,这是来自my view controller的更多代码,用于将用户输入的日期保存在选择器中:

- (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];

}

我已经在这段代码中钻研了三天了,但我不明白为什么我的通知在应该通过的时候没有通过。非常感谢您的帮助,谢谢

您是否理解UILocalNotification和远程通知是不同的东西

在didRegisterForRemoteNotificationsWithDeviceToken中,您可以获得用于远程通知的设备令牌。远程通知是从服务器发送的,因此您必须将该令牌保存在服务器上,然后使用服务器发送任何远程通知

你为什么不改变这一点:

localNotif.fireDate = [eventDate dateByAddingTimeInterval:-630*60];
为此:

localNotif.fireDate = [[NSDate date] dateByAddingTimeInterval:60];

然后本地通知会在一分钟内发出。问题可能是您正在设置的日期。

您是否了解UILocalNotifications和remote notifications是不同的东西

在didRegisterForRemoteNotificationsWithDeviceToken中,您可以获得用于远程通知的设备令牌。远程通知是从服务器发送的,因此您必须将该令牌保存在服务器上,然后使用服务器发送任何远程通知

你为什么不改变这一点:

localNotif.fireDate = [eventDate dateByAddingTimeInterval:-630*60];
为此:

localNotif.fireDate = [[NSDate date] dateByAddingTimeInterval:60];

然后本地通知会在一分钟内发出。问题可能是您正在设置的日期。

您如何知道您的通知不起作用

本地和推送通知是不同的。如果您的应用当前处于活动状态,则本地通知实际上不会显示警报消息。它将只调用UIApplicationLegateMethod

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

您如何知道您的通知不起作用

本地和推送通知是不同的。如果您的应用当前处于活动状态,则本地通知实际上不会显示警报消息。它将只调用UIApplicationLegateMethod

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

谢谢它发送警报,但它似乎根本不依赖于用户在选择器中输入的日期。谢谢。它发送警报,但它似乎根本不依赖于用户在选择器中输入的日期。是的,这应该是原因。无论如何,如果您想在这种情况下显示警报,您可以手动创建一个UIAlert,并在
-(void)application:(UIApplication*)application didReceiveLocalNotification:(UILocalNotification*)notification
方法中手动显示它。是的,这应该是原因所在。无论如何,如果您想在这种情况下显示警报,可以手动创建UIAlert,并在
-(void)application:(UIApplication*)application didReceiveLocalNotification:(UILocalNotification*)notification
方法中手动显示它。