UILocalNotification is deprecated in iOS 10. Use
UNNotificationRequest instead. A
UILocalNotification object specifies a notification that an app can schedule for presentation at a specific date and time.
- iOS 4.0–10.0Deprecated
- watchOS 2.0–3.0Deprecated
The operating system is responsible for delivering local notifications at their scheduled times; the app does not have to be running for this to happen. Although local notifications are similar to remote notifications in that they are used for displaying alerts, playing sounds, and badging app icons, they are composed and delivered locally and do not require connection with remote servers.
Local notifications are primarily intended for apps with timer-based behaviors and simple calendar or to-do list apps. An app that is running in the background may also schedule a local notification to inform the user of an incoming message, chat, or update. An app can have only a limited number of scheduled notifications; the system keeps the soonest-firing 64 notifications (with automatically rescheduled notifications counting as a single notification) and discards the rest.
When you create a local notification, you must specify either a specific date or a geographic region as the trigger for delivering the notification. Date-based notifications are delivered at the day and time you specify, and allowances can be made for time zone changes as needed. Region-based notifications are delivered when the user enters or exits the specified region. In both cases, you can specify whether the notifications are one-time events or can be rescheduled and delivered again.
After creating a
UILocalNotification object, schedule it using either the
presentLocalNotificationNow(_:) method of the
UIApplication class. The
scheduleLocalNotification(_:) method uses the fire date to schedule delivery; the
presentLocalNotificationNow(_:) method presents the notification immediately, regardless of the value of
fireDate. You can cancel one or more local notifications using the
cancelAllLocalNotifications() method of the
When the system delivers a local notification, several things can happen, depending on the app state and the type of notification. If the app is not frontmost and visible, the system displays the alert message, badges the app, and plays a sound—whatever is specified in the notification. If the notification is an alert and the user taps the action button (or, if the device is locked, drags open the action slider), the app is woken up or launched. (If the user taps one of the custom actions you specify using the
category property, the app is woken up or launched into the background.) In its
application(_:didFinishLaunchingWithOptions:) method, the app delegate can obtain the
UILocalNotification object from the launch options dictionary using the
localNotification key. The delegate can inspect the properties of the notification and, if the notification includes custom data in its
userInfo dictionary, it can access that data and process it accordingly. On the other hand, if the local notification only badges the app icon, and the user in response launches the app, the
application(_:didFinishLaunchingWithOptions:) method is called, but no
UILocalNotification object is included in the options dictionary. When the user selects a custom action, the app delegate’s
application(_:handleActionWithIdentifier:for:completionHandler:) method is called to handle the action.
If the app is foremost and visible when the system delivers the notification, the app delegate’s
application(_:didReceive:) is called to process the notification. Use the information in the provided
UILocalNotification object to decide what action to take. The system does not display any alerts, badge the app’s icon, or play any sounds when the app is already frontmost.
An app is responsible for managing the badge number displayed on its icon. For example, if a text-messaging app processes all incoming messages after receiving a local notification, it should remove the icon badge by setting the
applicationIconBadgeNumber property of the
UIApplication object to 0.