Chapter 16.  Implementing Rich Notifications

The previous chapter introduced you to App Extensions. You learned that App Extensions enable you to tightly integrate your app with iOS. In iOS 10, Apple has added extension points for notifications while completely revamping how notifications work.

In this chapter, we're going to explore how notifications work in iOS 10 and how you can implement extensions that will have a tremendous positive impact on the user experience of receiving notifications. We'll look at both local and remote notifications and how they have been unified in the new UserNotifications framework.

First, we're going to have a broad look at notifications and how they manifest throughout iOS. Even though we won't go into a lot of detail straight away, you'll gain a deep understanding of notifications by looking at the different types of notifications and the different methods of scheduling notifications that are available. Next, we'll have a detailed look at how your app can schedule and handle notifications. Finally, we'll look at the extension points that have been added by Apple in order to implement richer and more engaging notifications.

This chapter is divided into the following topics:

  • Gaining a deep understanding of notifications
  • Scheduling and handling notifications
  • Implementing notification extensions

By the end of this chapter, you'll know everything there is to know about notifications and how they can be used to create a unique, engaging user experience. People receive lots of notifications throughout the day, so providing a great experience that stands out will ensure that people don't get annoyed with your notifications to the point of wanting to disable them entirely.

Ready? Let's take a head-first, deep dive into notifications in iOS 10.

Gaining a deep understanding of notifications

Since you're planning on becoming a master at iOS development, you've probably used an iPhone or iPad more than a couple of times. It's also very likely that you've had applications sending you notifications while you were using the device. Or maybe while the device was sitting idle on your desk or somewhere else.

Notifications are a perfect way to inform users about information in your app that is of interest to them. New messages in a messaging app, breaking news events, or friend requests in social apps are just a few examples of great scenarios for notifications. From the get-go, it's important that you're aware of the fact that a lot of apps are fighting for the user's attention at all times. Notifications are a great way to gain the user's attention, but if you send too many notifications, or only use notifications as a marketing tool instead of a way to provide meaningful information, it's very likely that the user is going to disable push notifications for your app.

There are four forms of notifications that you can send to your users:

  • Sounds
  • Vibrations
  • Visual notifications (banners, alerts, and so on)
  • Badges

Most apps use a combination of these, and we'll go in-depth using a real-world scenario soon. To send notifications to your users, there are two mechanisms available: local notifications and push notifications. Local notifications are scheduled inside of your app and kept on the device. To trigger these notifications, you use either a location, calendar, or time-based trigger.

Push notifications are pushed to a device through a server and the Apple Push Notification Service (APNS). Push notifications are always delivered by APNS using best-effort delivery. This means that a notification will be sent to the user and, if this fails, the APNS may or may not try again. There are no guarantees made about this, so you should never assume that your notification did or did not end up on the user's device.

Push notifications can send either a user-facing notification or a silent notification. A user-facing notification will actively notify the user about what happened, often in the shape of a banner or badge icon. A silent notification signals the app that it should be woken up to perform a background fetch. This is a great way to complement the background fetch features you read about in Chapter 11 , Being Proactive with Background Fetch.

Before iOS 10, local and push notifications were hardly related to each other. You had to do a lot of duplicate work and it simply wasn't straightforward to work with notifications. In iOS 10, the UserNotifications framework is introduced. This framework unifies the registration, delegation, and handling of notifications. An added benefit of this approach is that the framework doesn't just work with iOS but also with watchOS and tvOS. Not all platforms support the same functionality, so if you plan to add notifications to a watch or TV app, you should make sure that everything will function as you expect.

The most exciting and impactful addition to notifications in iOS 10 is Notification Extensions. These extensions add a whole new dimension to notifications, especially for devices that support 3D-Touch. There are two types of extensions we can add: a Service Extension and a Content Extension. Service Extensions enable you to modify or augment the content of a notification before it's displayed to the user. Content Extensions enable you to display a custom interface when the user 3D-Touches a notification.

More on these extensions later. Now that you have a basic understanding of notifications and how they work in iOS, let's start with the basics of implementing notifications: scheduling and handling them.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.36.71