Generate a device token for iOS
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. 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.
- 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).
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.
1import UserNotifications23func requestNotificationPermission() {4UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in5if let error = error {6print("Error requesting notification permission: \(error)")7return8}910if granted {11DispatchQueue.main.async {12UIApplication.shared.registerForRemoteNotifications()13}14}15}16}
Call this method early in your app's lifecycle, such as in application(_:didFinishLaunchingWithOptions:).
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.
1func application(_ application: UIApplication,2didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {3let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()4// Send this token to Twilio using the DeviceRegistrations API5// or include it directly in a PushNotifications request.6}78func application(_ application: UIApplication,9didFailToRegisterForRemoteNotificationsWithError error: Error) {10print("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.
Info
Never cache the device token locally and assume it remains valid. Always use the token provided in the most recent delegate callback.
After you retrieve the token, include it in Twilio Push Notifications API requests using "provider": "APN". You can either:
- Send directly: Include the token in the
toarray when sending a notification. - Register the device: Store the token as a DeviceRegistration so you can send to Users by
userId.
- Send a push notification using the token.
- Obtain APNs credentials to upload to Twilio.
- Review Apple's Registering your app with APNs guide for additional details.