1

My question is with this notification I have for my application.

I am using a reminder notification for my music app, so people can get a notification two hours before I play a show. I tested this similar code earlier but with integer components rather than a NSString date, and it worked. However, I need to be using a string in this particular instance.

To me, everything looks like it should be working. The NSLog that's located within the singleton outputs the correct information. E.G. =

NSLog(@"Notification will be shown on: 2014-07-18 11:03:00 +0000)

So why am I not getting a notification? I've been trying for awhile, and have hit road blocks all day. Would appreciate some assistance.

Thank you!

- (void)scheduleNotificationForDate:(NSDate *)date {

    // Here we cancel all previously scheduled notifications
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

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

    localNotification.fireDate = date;
    NSLog(@"Notification will be shown on: %@",localNotification.fireDate);

    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.alertBody = [NSString stringWithFormat:@"Aurora Radio plays in 2 hours!"];
    localNotification.alertAction = NSLocalizedString(@"View details", nil);

    /* Here we set notification sound and badge on the app's icon "-1"
       means that number indicator on the badge will be decreased by one
       - so there will be no badge on the icon 
    */
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.applicationIconBadgeNumber = 1;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

IBAction=

-(IBAction)setReminder {

    // Setting all the information about our date
    NSString * dateString = @"2014-07-18 11:03:00";

    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSDate *myNewDate = [dateFormatter dateFromString:dateString];

    [self scheduleNotificationForDate:myNewDate];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Aurora Radio"
                                                message:@"Alarm has been set two hours prior to performance!"
                                               delegate:self
                                      cancelButtonTitle:@"Sounds good bros, see you later."
                                      otherButtonTitles:nil];
    [alert show];
}
Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
Machina
  • 252
  • 2
  • 13
  • Have you verified that your string is being converted to the correct NSDate value? – Hot Licks Jul 18 '14 at 16:25
  • maybe your application is not in the Notification Center and / or the notification are disabled for your application? – holex Jul 18 '14 at 16:26
  • HotLicks - Yes. Doesn't the NSLog verify that? @holex - It shouldn't be. I tested it previously using integer components, and was able to get a result. Except, in this case I need to use a string and not individual integers. – Machina Jul 18 '14 at 16:29
  • I'm just wondering where your device is _in_ the `GMT+0000` timezone? or you may live in different timezone? because you have set notification's date definitely for `GMT+0000` timezone, if you live a few timezones away, the date is not correct for the reminder – it should have been fired earlier as you expect or it will fire much later as you expect. – holex Jul 18 '14 at 16:32
  • @holex - I set the date definitely to GMT+0000 because if I DIDNT use it, I would get the following: (Notification will be shown on: 2014-07-18 16:03:00 +0000). See how it jumps up 5 hours? Doesn't make sense to me. – Machina Jul 18 '14 at 16:35
  • 1
    @Machina, so you live 5 hours away from the selected timezone – so, no wonder the notification's will be never fired because it is already outdated by 5 hours because you messed up the timezones. :) FYI: the `2014-07-18 16:03:00 +0000` is the exact same date as `2014-07-18 11:03:00 -0500`, and the `NSLog` always shows the date on the console in `+0000` timezone, therefore chuck away this line immediately: `[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];` and enjoy your application is finally works as it should. – holex Jul 18 '14 at 16:40
  • 1
    @holex is right, I bet the problem is with timezones. You can't assume that the user is in your timezone. In what timezone are you on right now? Don't forget, no matter what you do, NSDate will always work with UTC and will reflect that fact on its string representation. – galrito Jul 18 '14 at 16:40
  • @holex Wow, it was really that simple huh? Haha thank you very much! – Machina Jul 18 '14 at 19:02

0 Answers0