I do this in my app by using NSLocalNotification
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
if (localNotification == nil)
return;
localNotification.fireDate = dateToRemindOn;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
// details
localNotification.alertBody = @"Alert Message";
// Set the button title
localNotification.alertAction = @"View";
localNotification.soundName = UILocalNotificationDefaultSoundName;
// custom data for the notification to use later
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:reminderID forKey:@"remindID"];
localNotification.userInfo = infoDict;
// Schedule notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
This will create the local notification and you can store any information you might need in the user info dictionary and that will be available to you when it is received or opened.
Use this method in your AppDelegate to check if the app was opened from your local notification.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Handle launching from a notification
UILocalNotification *localNotification =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotification) {
//handle local notification
}
}
And use this method in your App Delegate to catch when a local notification is received while the app is open
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
// Handle notification when app is running
}