Beliebte Suchanfragen

Cloud Native



Agile Methoden



How To Set Up iOS 10 Local Notifications

14.11.2016 | 6 minutes of reading time

A short introduction to notifications

Notifications are part of iOS for a long time, but iOS 10 brings us a way to present custom notifications to the user.

Say, for example, that there is a large bubble gum store. When a client walks into a shop to buy a jar of bubble gums, user’s device that runs the BestBubblegums(tm) app, can get a notification with a picture of bubble gum saying that there is a discount if you buy 100 pieces or more. We need a way to inform bubble gum buyer of this so that he becomes inspired to buy the product.

Before iOS 10 we could inform our user’s only by sounds, app icon badges and alerts. That’s not very engaging in today’s emoticon-based bubble gum mobile world. In order to show our product to the user, we would have to open the app when the user interacts with the notification.

iOS 8 added interactivity and color-coded actions.

iOS 8 brought the possibility of attaching actions to notifications. Actions could be from liking a post, accepting attendance to an event to actions that requires opening an app and then performing some action. That was very handy to users because they can make decisions right from the notification view.

One thing that is also new since iOS 8 is the push notification payload size. Since iOS 8 and the later notification payload is increased from 256 bytes to 2 kilobytes, making notifications more versatile. We could use the notification payload, for example, to send headlines of all topics our user has subscribed to. Or we can send a list of users that started following post our user has made, without making calls to backend infrastructure, after push notification is received.

iOS 10

In iOS 10 notifications are way richer than they were in iOS 8. They can contain images, video and animations, so we can create more compelling notifications.

To see an extended notification, user needs to swipe down on the notification, which is on Springboard, or 3D touch on it. If the notification is on a lock screen, user needs to long press or swipe left to see revealed controls.

Notifications can be remote and local, and the latter is what this blog is about. We will briefly mention remote notifications, called push notifications. They are called push notifications because some remote infrastructure sends them to the device. In other words, the server sends notification and a device responds.

Apple has their own service for pushing notifications to devices which is called Apple Push Notification Service, or APNS. Remote notifications are first sent by our server, which notifies APNS which then delivers notification to a device. The client needs to have an infrastructure that can support scheduling push notifications. This is usually performed by the backend that services the application, but can also be delegated to other providers.

In contrast, local notifications are scheduled on users device and will be delivered to the user, depending on the way that they are scheduled.

Because notification content can be customised, we can make more interesting notifications, for example, we could put animated image and play music.

In custom notification, developers can use almost all UIViewController capabilities to create engaging content for the user.

Custom notifications can contain images, video, sound and custom generated content. Gif images are also supported as attachments, not as image in UIImageView. Adding gesture recognizers or UIResponder controls to view won’t work because processing touches is not available in content extension. Adding actions, which will appear as buttons below the notification content extension view, can guide user decision making. Rotating device also rotates notification view, so that needs to be taken into account when setting up autolayout constraints.

Let’s go through all the steps

First, Notification Content Extension needs to be created in Xcode.
This will create new target in Xcode project, with a blank storyboard to design notification, UIViewController subclass which implements UNNotificationContentExtension protocol and Info.plist file.
With these files, it is possible to add balloons to notifications.

Notification design can be done in storyboard now. UI elements and autolayout constraints are added, IBOutlets are connected, the same way as with any other UIViewController.

Content size ratio and extension category needs to be set for the notification to display correctly.

Info.plist file must have these keys:

  • UNNotificationExtensionInitialContentSizeRatio which specifies view controller height in relation to its width. It is used by the system to setup frame rectangle before content is loaded. Without this, the notification’s view may rapidly shrink or expand during appearance animation, and may partially hide or show more empty space than is needed.
  • UNNotificationExtensionCategory is the key which system uses to identify content for notification, because apps can have more notification content extensions.

Category object needs to be configured for notification. UNNotificationCategory is a class that tells the notification, which notification types it can handle. Actions for notification are attached to category object.

1let checkAction = UNNotificationAction.init(identifier: "CheckId", title: "Check Offer", options: [.foreground])
2let nextTimeAction = UNNotificationAction.init(identifier: "NextId", title: "Next Time", options: [.foreground])
3let bubblegumCategory = UNNotificationCategory.init(identifier: "CategoryId", actions: [checkAction, nextTimeAction], intentIdentifiers: [], options: [])

Let’s see how to schedule local notifications. Scheduling is done in UNNotificationTrigger subclass. There are three ways to schedule notification, represented by three subclasses:

  1. by specific amount of time. UNTimeIntervalNotificationTrigger class specifies the amount of time in seconds that must pass for notification to appear.
  2. by specifying information about the date on which notification needs to fire. UNCalendarNotificationTrigger class which carries temporal information. This can be used to notify user about someones birthday or scheduled meeting.
  3. by specifying a geographic region. Class for this is called UNLocationNotificationTrigger, and it contains CLRegion subclass object. Region object describes geographic region that Location Service will use to detect when the device has moved into that region. This can be used to notify user about a discount in a shop that is near him. This would require him to have our bubble gum app installed on his device.

This example uses timed notifications. Those balloon’s will have to wait for their time.

For local notification, UILocalNotification class was used in iOS 7 to schedule local notification. This class is not needed in iOS 10. A new UNUserNotificationCenter class was added to schedule both local and remote notifications.

Let’s schedule our cheap bubble gums notification with UNUserNotificationCenter class:

1let body: String? = "Cheap bubble gums! Only Today!"
3// we must configure content first
4let content = UNMutableNotificationContent.init()
5content.title = "Huge Discount!"
6content.body = body!
7content.categoryIdentifier = "CategoryId"
9// trigger is needed for the system to know when to fire notification
10let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 10.0, repeats: false)
12// be careful about request identifier, this uniquely identifies our notification
13// schedule several notifications with the same identifier, only the last one will be scheduled
14let request = UNNotificationRequest.init(identifier: "RequestId", content: content, trigger: trigger)
16// ask to schedule our notification
17UNUserNotificationCenter.current().add(request) { (error: Error?) in
18if error != nil {
19   print(error?.localizedDescription)

When the app is started, the user will be prompted by authorisation prompt about notification. User can choose to be notified about bubble gum discounts or not to be bothered at all, because he is playing a game in augmented reality.

And finally user buy cheap bubble gum because of nice notification.

You can check sample code here .

share post




More articles in this subject area

Discover exciting further topics and let the codecentric world inspire you.


Gemeinsam bessere Projekte umsetzen.

Wir helfen deinem Unternehmen.

Du stehst vor einer großen IT-Herausforderung? Wir sorgen für eine maßgeschneiderte Unterstützung. Informiere dich jetzt.

Hilf uns, noch besser zu werden.

Wir sind immer auf der Suche nach neuen Talenten. Auch für dich ist die passende Stelle dabei.