How to show local notification always at the same time?

Uplift iOS Interview

The Guide is for YOU if
  • You are preparing for an iOS interview and want to improve your skills and knowledge and looking to level up your interview game and land your dream job.
  • You want to gain confidence and ease during iOS interviews by learning expert tips and curated strategies.
  • You want access to a comprehensive list of iOS interview QA to practice and prepare.

For local notifications, you need to generate the notification content and provide a time or location. Both Local and remote notifications inform the user to do specific action by displaying a notification badge/alert with/without sound. The badge generally shows when the app is on background or not running state. The local notification remind user that he has some relevant things to do in the app. As notification is disruptive and it hurts user’s attention, you must ask for permission to show them.

The permission part is really simple and it is same as all the other type of permission in iOS –

UNUserNotificationCenter.current()
            .requestAuthorization(options: [.alert, .sound, .badge]) { granted, _  in
                // Do something when granted
            }

Let’s create notification content –

let content = UNMutableNotificationContent()
content.title = "Content title"
content.subtitle = "Some text"

Get ahead of the competition and ace your next iOS interview with expert-curated resources. Check out the collection of SwiftUI and Swift interview questions and answers, behavioral interview tips, and the popular custom layout contents. Plus, go through the new section for Engineering Manager to help you excel in your career!

Join my free Newsletter 🚀

Now create a trigger with a specific time. Here, the notification will be launched at the same(8.00AM) time daily.

var dateComponents = Calendar.current.dateComponents([.year, .month, .day], from: Date())
dateComponents.hour = 8
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)

Finally add the notification in the UNUserNotificationCenter to launch.

let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)

UNUserNotificationCenter.current().add(request) { error in
    if let error = error {
       print(error)
    }
}


✍️ Written by Ishtiak Ahmed

👉 Follow me on XLinkedIn



Get Ready to Shine: Mastering the iOS Interview




Enjoying the articles? Get the inside scoop by subscribing to my newsletter.

Get access to exclusive iOS development tips, tricks, and insights when you subscribe to my newsletter. You'll also receive links to new articles, app development ideas, and an interview preparation mini book.

If you know someone who would benefit from reading this article, please share it with them.