Class

UILocalNotification

Important: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.

Overview

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 scheduleLocalNotification(_:) or 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 cancelLocalNotification(_:) or cancelAllLocalNotifications() method of the UIApplication object.

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.

Topics

Scheduling a Local Notification

var fireDate: Date?

The date and time when the system should deliver the notification.

var timeZone: TimeZone?

The time zone of the notification’s fire date.

var repeatInterval: NSCalendar.Unit

The calendar interval at which to reschedule the notification.

var repeatCalendar: Calendar?

The calendar the system should refer to when it reschedules a repeating notification.

var region: CLRegion?

The geographic region that triggers the notification.

var regionTriggersOnce: Bool

A Boolean value indicating whether crossing a geographic region boundary delivers only one notification.

Composing the Alert

var alertBody: String?

The message displayed in the notification alert.

var alertAction: String?

The title of the action button or slider.

var alertTitle: String?

A short description of the reason for the alert.

var hasAction: Bool

A Boolean value that controls whether the notification shows or hides the alert action.

var alertLaunchImage: String?

Identifies the image used as the launch image when the user taps (or slides) the action button (or slider).

var category: String?

The name of a group of actions to display in the alert.

Configuring Other Parts of the Notification

var applicationIconBadgeNumber: Int

The number to display as the app’s icon badge.

var soundName: String?

The name of the file containing the sound to play when an alert is displayed.

var userInfo: [AnyHashable : Any]?

A dictionary for passing custom information to the notified app.

Constants

Notification Sound

The default system sound for local notifications.

Relationships

Inherits From

See Also

Deprecated Classes

class UIActionSheet

Important: UIActionSheet is deprecated in iOS 8. (Note that UIActionSheetDelegate is also deprecated.) To create and manage action sheets in iOS 8 and later, instead use UIAlertController with a preferredStyle of actionSheet.

Deprecated
class UIAlertView

In apps that run in versions of iOS prior to iOS 8, use the UIAlertView class to display an alert message to the user. An alert view functions similar to but differs in appearance from an action sheet (an instance of UIActionSheet).

Deprecated
class UIDocumentMenuViewController

A list of all the available document providers for a given file type and mode, in addition to custom menu items that you add.

Deprecated
class UIMutableUserNotificationAction

Important: UIMutableUserNotificationAction is deprecated in iOS 10. Use UNNotificationAction instead. A UIMutableUserNotificationAction object represents a modifiable version of the UIUserNotificationAction class.

Deprecated
class UIMutableUserNotificationCategory

Important: UIMutableUserNotificationCategory is deprecated in iOS 10. Use UNNotificationCategory instead. A UIMutableUserNotificationCategory object encapsulates information about custom actions that your app can perform in response to a local or push notification. Use instances of this class to customize the actions included in an alert when space onscreen is constrained.

Deprecated
class UIPopoverController

Important: UIPopoverController is deprecated in iOS 9. In iOS 9 and later, a popover is implemented as a UIViewController presentation. To create a popover, use an instance of UIPopoverPresentationController and specify the popover style.

Deprecated
class UISearchDisplayController

Important: UISearchDisplayController is deprecated in iOS 8. (Note that UISearchDisplayDelegate is also deprecated.) To manage the presentation of a search bar and display search results in iOS 8 and later, instead use UISearchController.

Deprecated
class UIStoryboardPopoverSegue

The UIStoryboardPopoverSegue class defines a specific type of segue for presenting content in a popover. For popover segues, the destination view controller contains the content to be displayed in the popover. This class provides an additional popoverController property so that your custom code has access to the popover controller object. For example, you might want to store the popover controller elsewhere in your code so that you can dismiss the popover programmatically.

Deprecated
class UIUserNotificationAction

Important: UIUserNotificationAction is deprecated in iOS 10. Use UNNotificationAction instead. A UIUserNotificationAction object represents a custom action that your app can perform in response to a remote or local notification.

Deprecated
class UIUserNotificationCategory

Important:UIUserNotificationCategory is deprecated in iOS 10. Use UNNotificationCategory instead. A UIUserNotificationCategory object encapsulates information about custom actions that your app can perform in response to a local or push notification.

Deprecated
class UIUserNotificationSettings

Important: UIUserNotificationSettings is deprecated in iOS 10. Use UNNotificationSettings instead. A UIUserNotificationSettings object encapsulates the types of notifications that can be displayed to the user by your app.

Deprecated