Chapter 12. Sending Notifications

When building an application, most of the interactions will take place while users are actually inside the app. Users are interacting with user interface elements, and your app can respond to their input in real time.

However, sometimes your app may receive information from a server, or be alerted that some scheduled action is happening while the user isn't inside the app. A lot of times, your app can just process these things the next time the app launches, like when your Facebook feed refreshes when you enter. However, other times the information coming in is time-sensitive, or of higher importance, and you need to let the user know what's going on, even if the app is closed

In these situations, you can send the user a notification. Notifications are presented to the user in many forms, and can provide a handful of ways for the user to respond to the information being presented. In this chapter, we're going to cover:

  • Local versus remote notifications
  • Scheduling local notifications
  • Creating categories and actions for notifications
  • Using badges and sounds
  • Writing code to handle incoming notifications

Introduction to user notifications

Chances are that if you've used an iOS device, you're very familiar with the user notification system. We'll review the components of notifications so that we're refreshed prior to coding, but before we do that it's worth taking a second to talk about notifications in iOS.

If you remember in the last chapter, when we were getting orientation changes, we used a class called NotificationCenter to register for notifications. These system notifications are completely different from user notifications, which are visual elements that alert a user about things going on inside an app. Those system notifications were strictly code, notifying us, as a programmer, about things going on in other parts of our application's code.

You'll see later on when we start coding that we are dealing with the UUNNotificationContentandUNNotificationTrigger objects for user alerts, as opposed to the Notification objects we used to be notified of coding events. It can be a little confusing to new programmers, so just keep it in mind that there are different meanings for the word notification.

Components of a user notification

A user notification can take many forms, depending on what makes sense for your app and what the user allows in their notification preferences. One of the most common forms is the banner, where the notification is presented at the top of the screen:

Components of a user notification

Figure 12.1: A banner notification

Some apps also use what are known as badges, the little red circles that appear in the corner of an app icon with a number letting the user know how many notifications are waiting for them. For apps that hold a number of actionable objects (like a mail or to-do app), a badge can add a lot of value:

Components of a user notification

Figure 12.2: A badge being shown in the corner of the built in messages app

Notifications that are received when the user's phone is locked can also be presented on the lock screen of the device. Lock screen notifications can also include a slide to reply action. These notifications allow the user to slide the notification to jump to a state of the application where they can act on the notification:

Components of a user notification

Figure 12.3: A notification on the lock screen with a slide action

Users can also swipe down from the top of their screen to access the notification center, which keeps a chronological list of all notifications that have not been read, and also lets them slide to act:

Components of a user notification

Figure 12.4: A notification in the notification center

As we move through the chapter, we'll learn how all of these capabilities are designed and implemented through the code in your application.

Local versus remote notifications

When working with notifications, there are two different types that we can use: local and remote notifications. Both look identical to the user, taking the forms we just discussed. The difference lies in how the notifications are created: local notifications are created and scheduled on the device, while remote notifications are delivered to the device from a server through a network connection.

Remote notifications are a bit complex; handling them as a notification is pretty much the same as a local notification, but actually setting up a server to create the notification and communicate with the Apple Push Notification (APN) service is beyond the scope of what we're covering in this book. Instead, we'll focus on local notifications.

By the end of the chapter you'll have the front end skills to handle notifications, so that later on in your career you could easily learn the additional skills required to fully use remote notifications.

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

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