Article

Registering Your App with APNs

Communicate with APNs and receive a unique device token that identifies your app.

Overview

Apple Push Notification service (APNs) must know the address of a user's device before it can send notifications to that device. This address takes the form of a device token unique to both the device and your app. At launch time, your app communicates with APNs and receives its device token, which you then forward to your provider server. Your server includes that token with any notifications it sends.

Enable the Push Notifications Capability

Apps must have the proper entitlements to use push notifications. To add these entitlements to your app, enable the Push Notifications capability in your Xcode project, as shown in Figure 1. Enabling this option in iOS adds the APS Environment Entitlement to the app. In macOS, it adds the APS Environment (macOS) Entitlement. See Enable push notifications in Xcode help for more information.

Figure 1

Enabling the push notifications capability

Add entitlements to your Xcode project by enabling the push notification capability in the Capabilities tab.

Register Your App and Retrieve Your App's Device Token

Register your app with APNs and receive a globally unique device token, which is effectively the address of your app on the current device. Your provider server must have this token before it can deliver notifications to the device.

You register your app and receive your device token each time your app launches using Apple-provided APIs. The registration process is similar across platforms:

In addition to handling successful registrations with APNs, be prepared to handle unsuccessful registrations by implementing the application(_:didFailToRegisterForRemoteNotificationsWithError:) method. Registration might fail if the user’s device isn't connected to the network, if the APNs server is unreachable for any reason, or if the app does not have the proper code-signing entitlement. When a failure occurs, set a flag and try to register again at a later time.

Listing 1 shows a sample implementation of the iOS app delegate methods needed to register for remote notifications and receive the corresponding token. The sendDeviceTokenToServer method is a custom method that the app uses to send the data to its provider server.

Listing 1

Communicating with APNs at launch time

func application(_ application: UIApplication,
           didFinishLaunchingWithOptions launchOptions:
           [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
   // Override point for customization after application launch.
               
   UIApplication.shared.registerForRemoteNotifications()
   return true
}

func application(_ application: UIApplication,
            didRegisterForRemoteNotificationsWithDeviceToken 
                deviceToken: Data) {
   self.sendDeviceTokenToServer(data: deviceToken)
}

func application(_ application: UIApplication,
            didFailToRegisterForRemoteNotificationsWithError 
                error: Error) {
   // Try again later.
}

Forward Tokens to Your Provider Server

Upon receiving a device token, open a network connection from your app to your provider server. Securely forward the device token and any other information you need to identify the specific user to your server. For example, you might include the user's login name or something that connects them to your service. Encrypt any information you send over the network.

On your provider server, store tokens in a secure place where you can access them to send notifications. When generating notifications, your server must be able to send notifications to specific devices. So if notifications are linked to a user's account, store device tokens with the user's account information. Because a user can have multiple devices, be prepared to handle multiple device tokens.

For information about how to send payloads and device tokens to APNs, see Sending Notification Requests to APNs.

See Also

Remote Notifications

Setting Up a Remote Notification Server

Set up a server to generate notifications and push them to user devices.