Local Notification Tutorial in iOS8 with Swift

Local notifications are a way for an application that isn’t running in the foreground to let its users know it has information for them. Local Notifications can display an alert message or they can badge the application icon. When users are notified they can launch the application and view the details. In this tutorial we will create a local notfication in the form of a message and a badge  on the application icon. This tutorial is built in iOS 8.1 and Xcode 6.1

Open Xcode and create a new Single View Application. For product name, use IOS8SwiftLocalNotificationTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.

Go to the Storyboard and drag a Button to the Main View and give it a title of "Send Local Notfication". The Storyboard should look like this.

Select the Assistant Editor and make sure the ViewController.swift is visible. Ctrl and drag from the Image to the ViewController class and create the following Action

Go to the ViewController.swift file and implement the sendNotification method

@IBAction func sendNotification(sender: UIButton) {
        var localNotification = UILocalNotification()
        localNotification.fireDate = NSDate(timeIntervalSinceNow: 5)
        localNotification.alertBody = "new Blog Posted at iOScreator.com"
        localNotification.timeZone = NSTimeZone.defaultTimeZone()
        localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1

Here we initialize the Local Notification object and we assign values to the following properties.

  • fireDate - The time when the notification is delivered. We set it here on 5 seconds.
  • alertBody - The message displayed in the notification alert.
  • timeZone - The time zone of the notfication's fire date
  • applicationIconBadgeNumber - The number to diplay on the icon badge. We will increment this number by one.

The notification is then scheduled with the scheduleLocalNotfication method. Go to AppDelegate.swift and change the application:didFinishWithLaunchingOptions to

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool { 
        // Override point for customization after application launch.
        if(UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:"))) {
            UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert | .Badge, categories: nil))
        return true

The first time the applications starts the user is asked for permission to show an alert or badge notification. if the application isn't running the user will see a banner displayed and a badge. By tapping on action button of the notification, users will launch the app. In this case, this method is called. Next, add the application:didReceiveLocalNotification method.

unc application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
        application.applicationIconBadgeNumber = 0

If the app is running while the notification is delivered, there is no alert displayed on screen. The application automatically calls this method.Again, the badge number is reset. Build and Run the project a dialog screen is displayed which asked for permission,

Select OK. Press the Local Notification button and the stop the project. You should see a banner displayed and an increment of the  badge number of the application icon. Click on the banner to go back to the application and to reset the badge number.

You can download the source code of the iOS8SwiftLocalNotificationsTutorial at the ioscreator repository on github.