0
votes

I'm writing an alarm clock iOS app. It's my first time using UILocalNotification. I'm getting the date from a date picker. I've formatted the dates to check whether my function was being passed the proper date, it was. I checked all of the needed properties for a UILocalNotification and I have them all and my notification still won't fire. Any ideas as to why? Thanks for the help.

#import "BIDAlarmViewController.h"

@interface BIDAlarmViewController () 

@end

@implementation BIDAlarmViewController

@synthesize datePicker; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib


}

- (void)didReceiveMemoryWarning{

[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(IBAction)setReminderUsingDateFromDatePicker: (id)sender{

[self scheduleNotificationForDate: datePicker.date];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm"];

NSString *formattedDateString = [dateFormatter stringFromDate:datePicker.date];


NSLog(@"Button Pressed.. date: %@", formattedDateString);

UIAlertView  *alert = [[UIAlertView alloc] initWithTitle:@"Alarm activated"
                                                 message:@"Alarm has been set"
                                                delegate:self cancelButtonTitle:@"OK"
                                       otherButtonTitles:nil];

[alert show];
}

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

UILocalNotification *alarm = [[UILocalNotification alloc] init];
if (alarm) {
    alarm.fireDate = date;
    alarm.timeZone = [NSTimeZone defaultTimeZone];
    alarm.repeatInterval = 0;
    alarm.soundName = @"alarmsound.caf";
    alarm.alertBody = @"Test message...";
    [[UIApplication sharedApplication] scheduleLocalNotification:alarm];
}
}

@end
1
Have you implemented the didReceiveLocalNotification method in your app delegate? - powerj1984
No, I haven't. I think I brushed over that. Thanks. - user1681673
Just out of curiosity, how did you expect to receive notification, if you had no method to receive it?? - Hot Licks
Probably just assumed it would show up in notification center - that's what I thought the first time I did that :) - powerj1984
@HotLicks I'm new to app development. Can you explain why that method needed to be in the app delegate to receive the notification? - user1681673

1 Answers

1
votes

http://developer.apple.com/library/ios/documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:didReceiveLocalNotification:

Make sure you've implemented the referenced method in your app delegate like so:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
  NSLog(@"Notification fired"!);
}

Notes from Apple on implementing this method:

Local notifications are similar to remote push notifications, but differ in that they are scheduled, displayed, and received entirely on the same device. An application can create and schedule a local notification, and the operating system then delivers it at the schedule date and time. If it delivers it when the application is not active in the foreground, it displays an alert, badges the application icon, or plays a sound—whatever is specified in the UILocalNotification object. If the application is running in the foreground, there is no alert, badging, or sound; instead, the application:didReceiveLocalNotification: method is called if the delegate implements it.

The delegate can implement this method if it wants to be notified that a local notification occurred. For example, if the application is a calendar application, it can enumerate its list of calendar events to determine which ones have due dates that have transpired or are about to transpire soon. It can also reset the application icon badge number, and it can access any custom data in the local-notification object’s userInfo dictionary.