1

Possible Duplicate:
UILocalNotification isn't working at all

I'm writing an app that sends the user an alert through the Notification Center when an event date is approaching. But when I set the date in the date picker and close the app, the notification doesn't appear. I already enabled Push Notifications in my provisioning profiles. This is all the code in my project that deals with the notification center,This is all the code in my view controller file dealing with the date picker:

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

}

And this is everything in my App delegate dealing with local notifications:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
....

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     UIRemoteNotificationTypeBadge |
     UIRemoteNotificationTypeAlert |
     UIRemoteNotificationTypeSound];

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"mm'/'dd'/'yyyy"];

    NSDate *eventDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"ImportantDatesViewController.selectedDate"];



    localNotif.fireDate = [eventDate dateByAddingTimeInterval:-13*60*60];
    localNotif.timeZone = [NSTimeZone defaultTimeZone];


    localNotif.alertBody = @"Event in three days!";

    localNotif.alertAction = nil;

    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 0;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];    

    return YES;  

}

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSString* pushToken = [[[[deviceToken description] 
                             stringByReplacingOccurrencesOfString:@"<"withString:@""] 
                            stringByReplacingOccurrencesOfString:@">" withString:@""] 
                           stringByReplacingOccurrencesOfString: @" " withString: @""];

    NSLog(@"%@", pushToken);

}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {

    NSLog(@"error: %@", error);
}

Any help is much appreciated, thank you!

Community
  • 1
  • 1
Henry F
  • 4,960
  • 11
  • 55
  • 98
  • 1
    I'm afraid I may have confused in the other question when I said that I use the name of the nib in the key when retrieving from `NSUserDefaults`. That's a matter of personal preference to ensure that the keys are unique. The truth of the matter is that you should be using whatever you used to store it, to retrieve it. Find where you're storing `eventDate` and use the same key. – Mark Adams Jan 22 '12 at 05:44
  • Thanks a lot Mark. I'm trying to determine where I'm storing eventDate, but I'm having some issues finding it. Is it possibly in the picker in my nib? – Henry F Jan 22 '12 at 05:54
  • 1
    In what view controller are you asking the user for this date that you want to fire the local notification? In that class, somewhere, you are storing a date into `NSUserDefaults`. Use the same key. Now you see why I mentioned storing keys as constants. :) – Mark Adams Jan 22 '12 at 05:57
  • @Mark Adams Oh definitely alrighty. I updated my OP with my viewDidLoad code and my action method I used to save the date. It looks like I should use `NSDate *eventDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"datePicker.selectedDate"];` but that doesn't seem to work either. – Henry F Jan 22 '12 at 06:30
  • You've just edited the code in your question, which means that the answers that are already here relate to the older code. Which makes it confusing. Also - If this is your view controller, is this view controller set as the application's delegate? Because if it isn't, then it isn't going to be receiving the app delegate's methods. – Abizern Jan 22 '12 at 17:52
  • Thanks a lot, I'll try that now. And it is all the exact same code, I just added my entire file in there for clarity. Thanks for your help, I'll try that now! – Henry F Jan 22 '12 at 17:57
  • I just totally rewrote the app, trying to place it in an IBAction now instead. It still isn't working. Instead of constantly updating this thread, and making other answers un-helpable to anyone else, I decided it would be best t just create a new one. – Henry F Jan 22 '12 at 20:46
  • See http://stackoverflow.com/a/4974553 – rishi Jan 22 '12 at 05:58
  • Oh okay thanks a lot. So, the alert will only appear if the app is not running at all, even in the background? Sorry for the dumb question I'm still pretty new to this. – Henry F Jan 22 '12 at 06:08
  • yes...that's true..it works only if app not running. you can following developer link for more information. http://developer.apple.com/library/IOs/#documentation/iPhone/Reference/UILocalNotification_Class/Reference/Reference.html. and when your issue is fixed, just accept the answer and close the question....and you will get to know all these soon... :) – rishi Jan 22 '12 at 06:12
  • Try this at the end of `-dateChanged:`... `[defaults synchronize];` – Mark Adams Jan 22 '12 at 06:52
  • @Mark Adams Thanks. I just added that in, the alert still didn't fire though. – Henry F Jan 22 '12 at 07:22
  • @RIP I was looking in my app delegate, and I actually don't have any method named `- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification` could that be the issue? – Henry F Jan 22 '12 at 20:47

1 Answers1

2

following code is use for the local notification.

-(IBAction)buttonPressed:(UIButton *)button
{
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];

    if (!localNotification)
        return;

    // Current date
    NSDate *date = [NSDate date]; 

    // Add one minute to the current time
    NSDate *dateToFire = [date dateByAddingTimeInterval:20];

    // Set the fire date/time
    [localNotification setFireDate:dateToFire];
    [localNotification setTimeZone:[NSTimeZone defaultTimeZone]];

    // Create a payload to go along with the notification
    NSArray *array = [NSArray arrayWithObjects:@"Value 1", @"Value 2", nil];
    NSDictionary *data = [NSDictionary dictionaryWithObject:array forKey:@"payload"];
    [localNotification setUserInfo:data];

    if (button == buttonAlert || button == buttonAll)
    {
        // Setup alert notification
        [localNotification setAlertBody:@"Incoming Local Notification" ];
        [localNotification setAlertAction:@"Open App"];
        [localNotification setHasAction:YES];
    }

    if (button == buttonBadge || button == buttonAll)
    {
        // Set badge notification, increment current badge value
        [localNotification setApplicationIconBadgeNumber:[[UIApplication sharedApplication] applicationIconBadgeNumber] + 1];
    }

    if (button == buttonSound || button == buttonAll)
    {
        // Setup sound notification
        [localNotification setSoundName:UILocalNotificationDefaultSoundName];
    }

    // Schedule the notification
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Nimit Parekh
  • 16,776
  • 8
  • 50
  • 72