Initialize Your iOS Apps for Push Notifications in 5-Steps

If you want to boost user engagement with your iOS Apps through- push notifications, you’re going to need to initialize support for notification receiving in your App.

Research found that users who opted in to push messages averaged 3x more app launches than those who opted out. To give users the ability to opt-in to use notifications in your iOS App requires initializing your app to use the UserNotifications framework of the iOS SDK, which includes the APIs for configuring and handling location and remote (push) notifications.

In our post Setup APNs for Your iOS App in 4 Easy Steps, we provided the steps to prepare company back-end servers to push data to an iOS app via the Apple Push Notification service. Here we provide an example of how to support receiving remote (push) notifications in your App using Swift.

iOS Apps must be configured to support remote notifications when launched. You can add the ability to do any of the following in response to an arriving notification:

  • Display a notification alert
  • Play a sound
  • Show a badge on the app icon

1. Import the User Notifications Framework

Begin by importing the UserNotifications In your UIApplicaiton delegate:

import UserNotifications

2. Request Authorization to Interact with the User

Users have to opt-in to to display alerts, play sounds, or badge the App’s icon. A user can grant or deny your request to authorize notifications, and can change the notifications settings for your App later in the system Settings App.

Typically configuration for notifications is in the application:didFinishLaunchingWithOptions: method of your UIApplication delegate by calling the requestAuthorizationWithOptions:completionHandler: method of the UNUserNotificationCenter object.

let center = UNUserNotificationCenter.current()

        center.requestAuthorization(options: [.badge, .alert, .sound]) { (granted, error) in

 // Enable or disable features based on authorization.

}

The first time that your App is launched, the system prompts the user to Allow the requested interactions.

3. Register with APNs

Next, your App registers with the Apple Push Notification Service (APNs) and receives a device token. This token is needed by your back end system to target notifications to a specific device.

To obtain a device token, call the registerForRemoteNotifications method of the UIApplication object as part of the normal launch sequence of your App:

UIApplication.shared.registerForRemoteNotifications()

4. Handle Remote Notification Registration

Your App is notified asynchronously about the successful or unsuccessful retrieval of the device token. You use App delegate callbacks to process the device token or to handle any errors that may have occurred.

func application(_ application: UIApplication,

    didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data){

        // Forward the token to your server.

}

    

func application(_ application: UIApplication,

    didFailToRegisterForRemoteNotificationsWithError error: Error) {

        // The token is not currently available.

}

After saving your device token on your back end server, you’ll be able to send notifications to your App on the specified iOS device.

5. Respond to Notifications

If a notification is received when your App is not running or in the background, the system automatically delivers the notification using the settings you request – an alert, sound and/or icon badge. If your App is running in the foreground, when a notification arrives it is delivered directly to your App. You can then decide whether to handle the notification quietly or alert the user. Our MobilizeWorx Notification Model canvas can help you determine the most effective user interactions in response to notifications.

To respond to the delivery of notifications, you must implement a delegate for the shared UNUserNotificationCenter object. Your delegate object must conform to the UNUserNotificationCenterDelegate protocol, which the notification center uses to deliver notification information to your app.

func userNotificationCenter(_ center: UNUserNotificationCenter,

                            didReceive response: UNNotificationResponse,

                            withCompletionHandler completionHandler: @escaping () -> Void) {

    if response.actionIdentifier == UNNotificationDismissActionIdentifier {

        // The user dismissed the notification without taking action

    }

    else if response.actionIdentifier == UNNotificationDefaultActionIdentifier {

        // The user launched the app

    }

    

    // Else handle any custom actions. . .

}

Download our iOS notifications starter Xcode 8 project to get a complete example of how to support receiving remote (push) notifications in an iOS App.

Download

You can download this for free, but if you feel like joining our mailing list for relevant articles, take a moment to fill in your information.


Leave a Reply

Your email address will not be published.

top