All posts
iOS

Notifications in iOS 10

Piotr Sochalewski

Piotr Sochalewski

With iOS 10, tvOS 10 and watchOS 3 launch Apple introduced the new UserNotifications framework. It supports the delivery and handling of local and remote notifications. Cool, but you can say that it was already possible through UIKit and classes like UILocalNotification or dictionaries for remote notifications. And you’re completely right, but these new possibilities, in particular, are extremely convenient and powerful. Take a look at it with me!

Let’s start with a little disclaimer: the new framework works great if you target your app for iOS 10 or newer. Otherwise, you have to use deprecated objects or implement it twice for iOS older and newer than 10.0. You can do this like that:

Okay, now when we know everything about the limitations, we can start with these shiny new features.

Notification state

Before you schedule the very first local notification, you should register your app for receiving user notifications at all.

First, remember to import UserNotifications. Then request authorization through the UNUserNotificationCenter singleton object.

I guess that you’ve already noticed that the new framework gives you information about the authorization status just after authorization. That’s something that was missing in the elder framework. You can also get even more precise status thanks to getNotificationSettings:.

The notificationSettings is an UNNotificationSettings object, which contains multiple enums giving the current status of an app notification settings. It’s pretty straightforward and let you do things that were not allowed in iOS 9.

Triggers

iOS 9 and elder don’t let you easily set repeating local notification. Fortunately, there were little libraries like our in-company Habit making this a bit easier.

Source: Introduction to Notifications – WWDC 2016

iOS Notification Triggers. Source: Introduction to Notifications – WWDC 2016

iOS 10 notifications use simple and powerful triggering mechanism. Let’s say you need an event that fires every hour. You can achieve that doing this:

Boring? What about a trigger based on location? This technique is used by Reminders.app and in iOS 10 it can look like this:

Then just use this trigger the same as in the previous example and you’ll receive a notification when to find yourself in 100 meters radius from our office!

Push notifications

You can use UNUserNotificationCenter‘s delegate to receive and handle push (and local – if you want) notifications.

Actions

iOS 10 notifications offer custom action. In the example, you can get a notification from your clinic about an upcoming visit and agree (confirm) or decline (resign) the term even without opening the app.

iOS 10 Notification Action

I’ve got a much simpler example with the action that doesn’t like to be tapped and if you’re stubborn, printing you boring lorem ipsum.

It’s all about setting notification categories with actions. In the example, I use only one category with one action inside. Then just set categoryIdentifier of your UNNotificationContent  to the proper identifier.

I also set up a delegate to the current view controller, because it works thank to userNotificationCenter(_:didReceive:withCompletionHandler:) method too.

UserNotificationsUI

I guess that’s not all about UserNotifications, but there is also UserNotificationsUI, which lets you customise the appearance of local and remote notifications. iOS 10 gives you the great opportunity to update your apps and many of us already did it (according to appreviewtimes.com average review time has increased from 1 to 3 days and I don’t think that’s because Apple fired some reviewers ;)). Why do not implement new notifications right now?

Newsletter

The post is created by Droids On Roids Team Member.
We would love to take care of your business.

Leave comment