简体   繁体   中英

iOS: Local Notifications Don't Fire On Time

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!

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:

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

to this:

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

Then the local notification should go off in a minute. Maybe the problem is the date that you are setting.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM