Reputation: 4980
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. It may be because of my objectForKey area. I have 2 nibs (one for iPhone, one for iPad) named ImportantDatesViewController_iPhone1 and ImportantDatesViewController_iPad1. Should I change the objectForKey area to the nib name instead of just "ImportantDatesViewController"? And my .h and .m file names are simply ImportantDatesViewController also. Sorry, I'm still very new at this and learning as I go. Here is all the code in my project that deals with the notification center, this is what I put in my view controller:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
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:-60*60*60];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = @"Event coming in three days!";
localNotif.alertAction = nil;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
return YES;
}
I also added this code in my didFinishLaunchingWithOptions method in the App Delegate at the bottom, and I thought it would do the trick:
[[UIApplication sharedApplication]registerForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];
Any help is much appreciated, thank you!
Upvotes: 0
Views: 2752
Reputation: 36447
Here is sample code for LocalNotification that worked for my project.
This code block in AppDelegate file :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];
// Override point for customization after application launch.
return YES;
}
// This code block is invoked when application is in foreground (active-mode)
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIAlertView *notificationAlert = [[UIAlertView alloc] initWithTitle:@"Notification" message:@"This local notification"
delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[notificationAlert show];
// NSLog(@"didReceiveLocalNotification");
}
This code block in .m file of any ViewController :
-(IBAction)startLocalNotification { // Bind this method to UIButton action
NSLog(@"startLocalNotification");
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:7];
notification.alertBody = @"This is local notification!";
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 10;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
The above code display an AlertView after time interval of 7 seconds when pressed on button that binds “startLocalNotification” If application is in background then it displays BadgeNumber as 10 and with default notification sound.
Upvotes: 1
Reputation: 1663
UILocalNotifications *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil) return;
NSDate *fireTime = [[NSDate date] dateByAddingTimeInterval:900] ; //15 min
localNotif.fireDate = fireTime;
localNotif.alertBody = @"Parking your CAR 15 Minutes reached..";
// localNotif.repeatInterval=kCFCalendarUnitMinute;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
Upvotes: 1
Reputation: 2261
Your telling it to fire a notification immediately when you set it and your setting it for a time before now by setting (NOW)-60*60*60.The notification has passed already.
[[UIApplication sharedApplication]presentLocalNotificationNow:localNotif];
if you want to set it for a specific time:
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
If the specified value is nil or is a date in the past, the notification is delivered immediately. As a side note make sure to set the timeZone and Locale to the ones you want.
Upvotes: 4