31

Local Notification iOS Tutorial

 5 years ago
source link: https://www.tuicool.com/articles/hit/qUJJZrQ
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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.

jMj2iuR.png!web

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.

JVFjYvB.png!web

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.

7NRBRvi.png!web

The Storyboard should look like this.

iQR7Jj2.png!web

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.

ymQJVzi.png!web

Go to the ViewController.swift file and import the UserNotifications framework

import UserNotifications

Change the viewDidLoad method to

override func viewDidLoad() {
  super.viewDidLoad()

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

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 theimage 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 Runthe Project. The user will be asked for permission.

m6zyYb7.png!web

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.

NneUvev.png!web

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK