← Back to list

How To Add Local Notification In iOS Apps Using Swift

Scheduling local notification you need to set an instance of UNNotificationSound to sound field

Abhay chaurasia · 2021-10-30 11:57 · 0 claps · 1.4 min read
#i̇os #swift #learning #read #devloper
Open on Medium ↗
Wiki topics: EDU · Education & Learning 📱 · Mobile Development

How To Add Local Notification In iOS Apps Using Swift

Scheduling local notification you need to set an instance of UNNotificationSound to sound field

Example:…

Step 1:

import UserNotifications

import userNotification in your Viewcontroller class.

Create a Function for call notification….

This function you can call in viewdidload for testing purpose….

func localNotification() {

UNUserNotificationCenter.current().delegate = self

UNUserNotificationCenter.current().getNotificationSettings { (notificationSettings) in

switch notificationSettings.authorizationStatus {

case .notDetermined:

self.requestAuthorization(completionHandler: { (success) in

guard success else { return }

self.scheduleLocalNotification()

// Schedule Local Notification

})

case .authorized:

self.scheduleLocalNotification()

break

// Schedule Local Notification

case .denied:

print(“Application Not Allowed to Display Notifications”)

case .provisional:

break

case .ephemeral:

break

@unknown default:

break

}

}

}

Step 2:

Create a request Authorization function….

local notification by default you can call this function in view did load

private func requestAuthorization(completionHandler: @escaping (_ success: Bool) -> ()) {

// Request Authorization

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (success, error) in

if let error = error {

print(“Request Authorization Failed ((error), (error.localizedDescription))”)

}

completionHandler(success)

}

}

Step 3:

Schedule the notification for set title , subtitle

private func scheduleLocalNotification() {

// Create Notification Content

let notificationContent = UNMutableNotificationContent()

// Configure Notification Content

notificationContent.title = Your title here…

notificationContent.subtitle = “You can write subtitle here..

notificationContent.body = “You can write content body here..

// guard let url = Bundle.main.url(forResource: “dr_calling”, withExtension: “mp3”) else {

// print(“error to get the mp3 file”)

// return

// }

notificationContent.sound = UNNotificationSound.init(named: UNNotificationSoundName(rawValue: “audioPlay.mp3”))

// Add Trigger

let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 5.0, repeats: false)

// Create Notification Request

let notificationRequest = UNNotificationRequest(identifier: “cocoacasts_local_notification”, content: notificationContent, trigger: notificationTrigger)

// Add Request to User Notification Center

UNUserNotificationCenter.current().add(notificationRequest) { (error) in

if let error = error {

print(“Unable to Add Notification Request ((error), (error.localizedDescription))”)

}

}

}

These are methods. you can use for add local notification in your project . thank you….


메타데이터
post_id
8fc10afc8b20
slug
how-to-add-local-notification-in-ios-swift-8fc10afc8b20
url
https://medium.com/@ac37636/how-to-add-local-notification-in-ios-swift-8fc10afc8b20
canonical_url
https://medium.com/@ac37636/how-to-add-local-notification-in-ios-swift-8fc10afc8b20
author_url
https://medium.com/@ac37636
status
ok
fetched_at
2026-07-27 21:01:28