Local Notification iOS 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 10 and built for iOS 12.

Open Xcode and create a new Single View App.

For product name, use IOSLocalNotificationTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and choose Next.

Go to the Storyboard and drag a Button to the Main View and give it a title of "Send Local Notfication".  Select the Resolve Auto Layout Issues button and select Reset to Suggested Constraints.

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 import the UserNotifications framework

import UserNotifications

Change the viewDidLoad method to

override func viewDidLoad() {

  UNUserNotificationCenter.current().requestAuthorization(options: [.alert]) {
      (granted, error) in
      if granted {
      } else {

The UNUserNotificationCenter manages the notification-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 inside the project folder. Next, implement the sendNotification action method

@IBAction func sendNotification(_ sender: Any) {
    // 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 IOSLocalNotificationTutorial at the ioscreator repository on Github