iOS Life

  • Home
  • ABOUT
  • SERVICES
  • PORTFOLIO
  • BLOG
  • Contact

Tutorial: Local Notifications

I know Push Notifications is all the rage lately, but let’s not forget about Local Notifications. They are much easier to implement and can sometimes be just as effective in getting Users to come back to your app. The short tutorial below will describe the steps needed to setup Local Notifications in your app.

Setup your Local Notification

The method below will set the notification to fire at a specific time. It doesn’t matter if the user has your app open or not. The notification will show up like most do on the device, usually a banner at the top of the screen that includes your app icon and a message. Users can change the alert style within “General Settings > Notifications > Your App Name”. Your notification will also appear in the device Notification Center.

-(void) setLocalNotification {
    
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setYear:2013];
    [comps setMonth:05];
    [comps setDay:01];
    [comps setHour:12];
    [comps setMinute:0];
    [comps setTimeZone:[NSTimeZone systemTimeZone]];
    NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *setTime = [cal dateFromComponents:comps];
    
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
        return;
    localNotif.fireDate = setTime;
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
	
    // Notification details
    localNotif.alertBody = @"Don't forget to come back to the app name today for your bonus!";
    // Set the action button
    localNotif.alertAction = @"View";
	
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 1;
	
    // Schedule the notification
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}

To customize the code above change the NSDate entry to the date and time you would like your notification to fire. This method could easily be modified to take an NSDate parameter to allow you to call this method based on a dynamic date and time. Next you’ll want to change the alertBody to the message you want the user to see. It should contain a call to action so the user will be compelled to tap it to return to your app. SoundName can be changed to to accept an mp3 or wav if you wish. The example above uses the default sound setup on the users device. The application icon red badge bubble is then set to one to notify the user that their is a notification for you app that has not yet been tapped by them.

Cancelling All Notifications

You may want to include a toggle within your app to allow the user to turn off/on notifications. If the user turns notification off you should remove all notifications set so the user doesn’t receive them. Use the method below to remove all local notifications. If the user turns notifications back on then you’ll need to loop through your data recursively using the method above.

    // Cancel all Notifications
    UIApplication* app = [UIApplication sharedApplication];
    NSArray *notifications = [app scheduledLocalNotifications];
    if ([notifications count] > 0)
       [app presentLocalNotificationNow:notifications[0]];

Receiving the Notification

When a user taps on the notification they receive your app needs to handle it. irst the red bubble indicator on the app icon needs to be reset to zero. Then your app needs to handle the notification. This could be showing a specific view within your app (i.e. Give a user something free for coming back to your app if it’s a game). If you just want your app to open as normal, use the method below. This is handled in your app delegate.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    
    self.mainVC = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
    self.window.rootViewController = self.mainVC;
    [self.window makeKeyAndVisible];
    
    // Reset the Icon Alert Number back to Zero
    application.applicationIconBadgeNumber = 0;
    
    // Detect the Notification after a user taps it
    UILocalNotification *localNotif =
    [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (localNotif) 
    {
	NSLog(@"Recieved Notification %@",localNotif);
    }
    
    return YES;
}

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
	// Handle the notification when the app is running
	NSLog(@"Recieved Notification %@",notif);
}
Mar 8, 2013 Brian Coleman
Tutorial: Setting up a Jenkins Automated Build Server for iOSFramework: MTLabel
You Might Also Like
 
Tutorial: Provision Your App to Run on a Device
 
Tutorial: Post to Web Server API in Swift using NSURLConnection
9 years ago Tutorialsios, objective-c, tutorial5,793
Follow Me
    
Categories
  • About Me
  • Frameworks
  • My Apps
  • News
  • Strategy
  • Swift
  • Tools
  • Tutorials
  • tvOS
  • Uncategorized
  • Videos
  • Watch
Archives
  • May 2016
  • January 2016
  • October 2015
  • July 2015
  • May 2015
  • April 2015
  • March 2015
  • November 2014
  • October 2014
  • September 2014
  • July 2014
  • June 2014
  • September 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
brianjcoleman on Twitter
  • Classix is still holding in the top charts on Apple TV in Canada. #55 Free, #23 Top Grossing. #tvos #appletv #app https://t.co/xuEJiT4rro, Jul 14
  • New Blog Post: "Classix for iPhone, iPad & Apple TV” #iOSDev #ios #swift #swiftlang #SwiftDevs #AppleTV Read here: https://t.co/uF6w3gYOot, May 20
  • New Blog Post: "How to test your app for IPv6 compatibility” #iOSDev #ios #swift #swiftlang #SwiftDevs Read here: https://t.co/SveichSUep, May 6

Recent Posts
  • Classix for iPhone, iPad & Apple TV
  • Tutorial: How to test your app for IPv6 compatibility
  • Tutorial: Testing SSL using Charles Proxy on an iOS Device
  • Tutorial: 3D Touch – Quick Actions in Swift
  • tvOS Tutorial: Top Shelf in Swift
Featured Apps
Classix
Sportsnet
TAGS
tutorialswiftios8iosobjective-cvideostrategygamesframeworknewsappsmonitizeios7applefacebookwatchtoolstvosios9bookdesignsocialapiprovisiontutorialsbooksiapiTunes ConnectIPv6
Search
ABOUT
Brian is a Lead iOS/tvOS Developer from Toronto with over 18 years of multifaceted experience including development, design, business analysis and project management.
MOST VIEWED
Tutorial: How To Use Login in Facebook SDK 4.1.x for Swift
163,312 views
Tutorial: How to test your app for IPv6 compatibility
101,670 views
Tutorial: How to use Auto Layout in Xcode 6
89,134 views
FOLLOW ME
    
Email Subscription
Sign up for my newsletter to receive the latest news and tutorials posted.

Enter your email address:

2013-2017 © Brian Coleman