Local notification not firing from calendar components
I made a local notification to launch at a specific time, but when I open
the app the notification already launches, I know this because of a NSLog.
This is my notification and calendar.
- (void)viewDidLoad
{
[super viewDidLoad];
NSCalendar *notificationCalender = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *notificationComponents = [notificationCalender
components: NSYearCalendarUnit | NSMonthCalendarUnit |
NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |
NSSecondCalendarUnit fromDate:[NSDate date]];
[notificationComponents setYear:2013];
[notificationComponents setMonth:8];
[notificationComponents setDay:31];
[notificationComponents setHour:4];
[notificationComponents setMinute:17];
UIDatePicker *notificationDatePicker = [[UIDatePicker alloc] init];
[notificationDatePicker setDate:[notificationCalender
dateFromComponents:notificationComponents]];
UIApplication *habitPal = [UIApplication sharedApplication];
UILocalNotification *postureNotification = [[UILocalNotification
alloc] init];
if (postureNotification) {
postureNotification.alertBody = @"Notification?";
postureNotification.timeZone = [NSTimeZone defaultTimeZone];
postureNotification.fireDate = notificationDatePicker.date;
[habitPal scheduleLocalNotification:postureNotification];
NSLog(@"Notification Launched");
}
}
And the it logs "Notification Launched" on the startup of the app. Just to
make sure, I set it so the fire date to
postureNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:3];
and it still logs "Notification Launched" on startup, so I think something
else is wrong. What am I doing wrong?
Edit
Ok, now I see you cant put this in the viewDidLoad, so I put it in the
appDelegate
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data,
invalidate timers, and store enough application state information to
restore your application to its current state in case it is terminated
later.
// If your application supports background execution, this method is
called instead of applicationWillTerminate: when the user quits.
NSCalendar *notificationCalender = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *notificationComponents = [notificationCalender
components: NSYearCalendarUnit | NSMonthCalendarUnit |
NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |
NSSecondCalendarUnit fromDate:[NSDate date]];
[notificationComponents setYear:2013];
[notificationComponents setMonth:8];
[notificationComponents setDay:31];
[notificationComponents setHour:5];
[notificationComponents setMinute:5];
UIDatePicker *notificationDatePicker = [[UIDatePicker alloc] init];
[notificationDatePicker setDate:[notificationCalender
dateFromComponents:notificationComponents]];
UIApplication *habitPal = [UIApplication sharedApplication];
UILocalNotification *postureNotification = [[UILocalNotification
alloc] init];
if (postureNotification) {
postureNotification.alertAction = @"Answer";
postureNotification.alertBody = @"Do you have good posture?";
postureNotification.timeZone = [NSTimeZone defaultTimeZone];
postureNotification.fireDate = notificationDatePicker.date;
[habitPal scheduleLocalNotification:postureNotification];
NSLog(@"Notification Launched");
}
}
But the notification launches right away after closing the app
No comments:
Post a Comment