Local Notification Tutorial

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. In iOS 10 Apple introduces rich notifications, which can include different type of media . In this tutorial we will create a local notification including an image. This tutorial is made with Xcode 8 and built for iOS 10.

Open Xcode and create a new Single View Application.

Choose Next. For product name, use IOS10LocalNotificationTutorial 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".  Select the button and select the Align Auto Layout Button. Select Horizontally in Container and choose "Items of New Constraints" in the "Update Frames" dropdown box. Select "Add 1 Constraint".

Next, while the button is still selected, select The Auto Layout Pin button. Pin the button to the top. Choose "Items of New Constraints" in the "Update Frames" dropdown box. Select "Add 1 Constraint".

The Storyboard should look like this.

Open the Assistant Editor and make sure the ViewController.swift file is visible. Ctrl and drag from Button to the ViewController class to create the following Action.

Go to the ViewController.swift file and change the viewDidLoad method to

override func viewDidLoad() {
    // Do any additional setup after loading the view, typically from a nib.
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert]) { (success, error) in
        if success {
        } else {

The UNUserNotificationCenter manages the notifcation-related activities. To use the notification the user needs to be asked for permission with the requestAuthorization method.

An image will be used as an attachment of the notification. Download the image and drag it to the project to import it. Next, implement the sendNotification action method

 @IBAction func sendNotification(_ sender: AnyObject) {
    // 1
    let content = UNMutableNotificationContent()
    content.title = "Notification Tutorial"
    content.subtitle = "from ioscreator.com"
    content.body = " Notification triggered"
    // 2
    let imageName = "applelogo"
    guard let imageURL = Bundle.main.url(forResource: imageName, withExtension: "png") else { return }
    let attachment = try! UNNotificationAttachment(identifier: imageName, url: imageURL, options: .none)
    content.attachments = [attachment]
    // 3
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10, repeats: false)
    let request = UNNotificationRequest(identifier: "notification.id.01", content: content, trigger: trigger)
    // 4  
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
  1.  The UNMutableNotificationContent object contains the data of the notification.
  2.  The UNNotificationAttachment object contains the media content of the notification.
  3.  An UNNotificationRequest is generated which will trigger at the timeinterval of 10 seconds.
  4. The notification is scheduled for delivery.

Build and Run the Project. The user will be asked for permission.

Select Allow, select the "Send Local Notification" Button to schedule the Notification. Press the Home button in the Simulator(Shift + Command + H). Within 10 seconds the Notfication will be delivered.

You can download the source code of the IOS10LocalNotificationTutorial at the ioscreator repository on Github.