0
0
iOS Swiftmobile~5 mins

Local notifications in iOS Swift

Choose your learning style9 modes available
Introduction

Local notifications let your app send alerts to the user even when the app is not open. They help remind or inform users about important things.

Remind a user about a scheduled event like a meeting or a task.
Notify the user about a new message or update while the app is in the background.
Alert the user to take an action, like completing a workout or checking a sale.
Provide timely information like weather alerts or news updates.
Encourage user engagement by sending friendly reminders.
Syntax
iOS Swift
import UserNotifications

let center = UNUserNotificationCenter.current()

// Request permission
center.requestAuthorization(options: [.alert, .sound]) { granted, error in
  // Handle permission
}

// Create content
let content = UNMutableNotificationContent()
content.title = "Title"
content.body = "Body text"

// Create trigger
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)

// Create request
let request = UNNotificationRequest(identifier: "id", content: content, trigger: trigger)

// Add request
center.add(request) { error in
  if let error = error {
    print("Error adding notification: \(error)")
  }
}

You must ask the user for permission before sending notifications.

Triggers decide when the notification appears (time, location, calendar).

Examples
Set the notification title and message.
iOS Swift
content.title = "Reminder"
content.body = "Don't forget to drink water!"
Trigger notification after 60 seconds, only once.
iOS Swift
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: false)
Trigger notification every day at 8:00 AM.
iOS Swift
let trigger = UNCalendarNotificationTrigger(dateMatching: DateComponents(hour: 8, minute: 0), repeats: true)
Sample App

This app asks permission to send notifications when it starts. If allowed, it schedules a notification to appear after 10 seconds with a simple message.

iOS Swift
import UIKit
import UserNotifications

class ViewController: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    let center = UNUserNotificationCenter.current()
    center.requestAuthorization(options: [.alert, .sound]) { granted, error in
      if granted {
        self.scheduleNotification()
      }
    }
  }

  func scheduleNotification() {
    let content = UNMutableNotificationContent()
    content.title = "Hello!"
    content.body = "This is your local notification."

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10, repeats: false)

    let request = UNNotificationRequest(identifier: "testNotification", content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request) { error in
      if let error = error {
        print("Error adding notification: \(error)")
      }
    }
  }
}
OutputSuccess
Important Notes

Notifications only appear if the user grants permission.

Test notifications on a real device for best results.

Notifications can include sounds, badges, and custom actions.

Summary

Local notifications alert users even when the app is closed.

Always request permission before scheduling notifications.

Use triggers to control when notifications appear.