Skip to contentSkip to navigationSkip to topbar
Page toolsOn this page
Looking for more inspiration?Visit the

Generate a device token for iOS


(new)

Beta

The Push Notifications API is currently available as a Private Beta product. The information contained in this document is subject to change. You acknowledge and agree that your use of the Twilio Push Notifications API is subject to the terms of the Services in Private Beta(link takes you to an external page). This means that some features are not yet implemented and others may be changed before the product is declared as Generally Available. Private Beta products are not covered by the Twilio Support Terms or Twilio Service Level Agreement.

To send push notifications to iOS devices with Twilio, your app needs an Apple Push Notification service (APNs) device token. This token uniquely identifies the app instance on a device. You then send this token to Twilio by including it in the token field when sending a notification or registering a device.


Prerequisites

prerequisites page anchor
  • An Xcode project with a valid Apple Developer account.
  • A physical iOS device (the iOS simulator does not support push notifications).
  • The Push Notifications capability enabled in your Xcode project (select your target, go to Signing & Capabilities, and click + Capability to add Push Notifications).

Request notification permission

request-notification-permission page anchor

Before registering for remote notifications, request the user's permission to display alerts, sounds, and badges. Call requestAuthorization on UNUserNotificationCenter and, if the user grants permission, register for remote notifications.

1
import UserNotifications
2
3
func requestNotificationPermission() {
4
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
5
if let error = error {
6
print("Error requesting notification permission: \(error)")
7
return
8
}
9
10
if granted {
11
DispatchQueue.main.async {
12
UIApplication.shared.registerForRemoteNotifications()
13
}
14
}
15
}
16
}

Call this method early in your app's lifecycle, such as in application(_:didFinishLaunchingWithOptions:).


Retrieve the APNs device token

retrieve-the-apns-device-token page anchor

After you call registerForRemoteNotifications(), the system contacts APNs and returns a device token through the AppDelegate. Implement the following delegate methods to handle the result.

1
func application(_ application: UIApplication,
2
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
3
let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
4
// Send this token to Twilio using the DeviceRegistrations API
5
// or include it directly in a PushNotifications request.
6
}
7
8
func application(_ application: UIApplication,
9
didFailToRegisterForRemoteNotificationsWithError error: Error) {
10
print("Failed to register for remote notifications: \(error)")
11
}

The deviceToken is returned as raw Data. The code above converts it to a hexadecimal string, which is the format Twilio expects in the token field.


APNs device tokens can change over time. iOS calls application(_:didRegisterForRemoteNotificationsWithDeviceToken:) each time the token is updated, including on every app launch. Always send the latest token to your server to ensure Twilio can continue delivering notifications.

(information)

Info

Never cache the device token locally and assume it remains valid. Always use the token provided in the most recent delegate callback.


Send the token to Twilio

send-the-token-to-twilio page anchor

After you retrieve the token, include it in Twilio Push Notifications API requests using "provider": "APN". You can either: