iOS Push Notifications Tutorial: Getting Started

  • By: Sujatha
  • Date: 27-04-2018

As a user of mobile devices and apps, you would have definitely come across "push notifications". These are real-time alerts that are used to convey a message to the user. A recent survey reported that out of several thousand consumers surveyed, more than 70% of them said that these push notifications were valuable to them, and helped them make a decision. This number is not at all surprising, given the fact that businesses these days are trying to connect and reach closest to their customers through personalized interactions, and app push notifications are one of the best ways to deal with it.

Generally, notifications are of 2 types. Local notifications and remote notifications. These are the ways to inform users when new data becomes available for a particular app, even when the app is not running in the foreground.

For example, a messaging app might let the user know when a new message has arrived, and a calendar app might inform the user of an upcoming event.

Local Notification: For a local notification, your app configures the notification details locally and passes those details to the system, which then handles the delivery of the notification when your app is not in the foreground. Local notifications are supported on iOS, tvOS, and watchOS.

Remote notification: You use one of your company’s servers to push data to user devices via the Apple Push Notification service. Remote notifications are supported on iOS, tvOS, watchOS, and macOS.

When to Use Local Notifications

Because apps on iOS are not always running, local notifications provide a way to alert the user when your app has new information to present.

For example, an app that pulls data from a server in the background can schedule a local notification when some interesting piece of information is received. Local notifications are also well suited for apps such as calendar and to-do list apps that need to alert the user at a specific time or when a specific geographic location is reached.

When to Use Remote Notifications

Remote notifications are appropriate when some or all of the app’s data is managed by your company’s servers. With remote notifications, you decide when you want to push a notification to the user’s device. For example, a messaging app would use remote notifications to let users know when new messages arrive. Because they are sent from your server, you can send remote notifications at any time, including when the app is not running on the user’s device.

Managing Notifications

Apps must be configured at launch time to support local and remote notifications. Specifically, you must configure your app in advance if it does any of the following:

  • Displays alerts, play sounds or badges its icon in response to an arriving notification.
  • Displays custom action buttons with a notification.

Requesting authorization for user interactions

You have to configure the notification support on your Appdelegate class in the method of

 application(_ application: UIApplication, 
didFinishLaunchingWithOptions launchOptions:


Request Authorization:

let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
   // Enable or disable features based on authorization.
}

Creating and registering a notification category

let generalCategory = UNNotificationCategory(identifier: "GENERAL",
                                            actions: [],
                                            intentIdentifiers: [],
                                            options: .customDismissAction)
 
// Register the category.
let center = UNUserNotificationCenter.current()
center.setNotificationCategories([generalCategory])

Defining custom actions for a category

let generalCategory = UNNotificationCategory(identifier: "GENERAL",
                                            actions: [],
                                            intentIdentifiers: [],
                                            options: .customDismissAction)
 
// Create the custom actions for the TIMER_EXPIRED category.
let snoozeAction = UNNotificationAction(identifier: "SNOOZE_ACTION",
                                       title: "Snooze",
                                       options: UNNotificationActionOptions(rawValue: 0))
let stopAction = UNNotificationAction(identifier: "STOP_ACTION",
                                     title: "Stop",
                                     options: .foreground)
 
let expiredCategory = UNNotificationCategory(identifier: "TIMER_EXPIRED",
                                            actions: [snoozeAction, stopAction],
                                            intentIdentifiers: [],
                                            options: UNNotificationCategoryOptions(rawValue: 0))
 
// Register the notification categories.
let center = UNUserNotificationCenter.current()
center.setNotificationCategories([generalCategory, expiredCategory])

Subscribe For Our Newsletter And Stay Updated

Recent blogs

thunder for drupal
Digital evolution has taken the world of professional publishing by storm. However, this evolution brought along a whole new set of...
higher education
Over the past 2 decades, the advancements in technology have been tremendous and these changes have played a major role in allowing...
DrupalCamp 2018
“Everyone you will ever meet knows something you don’t” - said Bill Nye popularly known as The Science Guy, who (coincidentally)...

This website uses cookies to offer you an enhanced website experience. We collect information about how you interact with our website to provide personalized services to you. To find out more, see our Privacy Policy

×