Skip to content
tezvyn:
Menu

UserNotifications Framework: iOS Alert Gatekeeper

EasyHow cards are made

UserNotifications is the gatekeeper between your app and the lock screen. You use it to request permission and schedule local or push alerts on iOS. Send a notification before authorization and the system silently drops it.

Why it exists

iOS is built around user privacy and control. Apple does not want apps interrupting users without explicit consent. Before UserNotifications, alerts were chaotic and untrusted. This framework centralizes every interruption into a single permission system so the user decides which apps can badge the home screen, play sounds, or show banners.

The mental model

Think of UserNotifications as the post office and the bouncer combined. Your app writes a letter, the post office schedules delivery, but the bouncer at the door checks if your app is even allowed onto the property. If the user said no, the letter never leaves the back room. There is no appeal and no error thrown; the message simply disappears.

How it works

You start by requesting authorization through UNUserNotificationCenter, usually at a context that makes sense such as after the user completes a welcome flow. You specify which interrupt types you want, such as alerts, sounds, or badges. Once granted, you construct a UNMutableNotificationContent object to set the title and body, attach a UNTimeIntervalNotificationTrigger or UNCalendarNotificationTrigger for local alerts, and wrap both in a UNNotificationRequest with a unique identifier. You then pass that request to the notification center. For remote push, the framework receives the payload from Apple Push Notification service and hands it to your app delegate or notification service extension.

When to use it

Use this framework for any user-facing interruption that originates from your app. That includes daily reminder apps, messaging apps that need to badge unread counts, fitness apps that alert you to stand up, or e-commerce apps sending delivery updates. If you need to wake the app from the background to fetch data, combine remote notifications with the background fetch capability.

When not to use it

Do not use UserNotifications for purely in-app events that never leave the current screen, such as a timer running inside the app while it is foreground. Do not rely on it for guaranteed delivery because the user can revoke permission at any time in Settings, and iOS may throttle or delay notifications based on battery or Focus modes.

One canonical example

A hydration tracking app wants to remind the user to drink water every two hours. On first launch, it calls requestAuthorization with the alert and sound options. After the user taps Allow, the app creates a UNMutableNotificationContent with the title Time to Hydrate and body Drink a glass of water. It pairs that content with a UNTimeIntervalNotificationTrigger set to 7200 seconds and adds the request to the current notification center. If the user denied permission, the app silently skips scheduling and shows an in-app reminder instead.

Interview question

What happens when your app schedules a notification before the user has granted authorization?

  • a.The system queues it and delivers once the user grants permission later
  • b.iOS automatically displays a system alert requesting permission when the notification fires
  • c.The system silently drops the notification without delivering itCorrect
  • d.The framework throws a runtime error that you must catch before scheduling
Why?

The card states that notifications scheduled before authorization are silently dropped with no error thrown. A is tempting but wrong because the framework does not queue unauthorized notifications for later delivery.

Just read this? Test yourself on what you have been reading.

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on ios — each one lists the topics its interview covers.

See open roles