# Generate a device token for iOS

> \[!IMPORTANT]
>
> 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](https://www.twilio.com/en-us/legal/service-country-specific-terms/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](/docs/push-notifications/api/notification-resource#send-a-pushnotification) or [registering a device](/docs/push-notifications/api/device-registration-resource#register-a-device).

## Prerequisites

* 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

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.

```swift
import UserNotifications

func requestNotificationPermission() {
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
        if let error = error {
            print("Error requesting notification permission: \(error)")
            return
        }

        if granted {
            DispatchQueue.main.async {
                UIApplication.shared.registerForRemoteNotifications()
            }
        }
    }
}
```

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

## Retrieve the APNs device token

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.

```swift
func application(_ application: UIApplication,
                 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
    // Send this token to Twilio using the DeviceRegistrations API
    // or include it directly in a PushNotifications request.
}

func application(_ application: UIApplication,
                 didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print("Failed to register for remote notifications: \(error)")
}
```

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.

## Handle token refresh

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.

> \[!NOTE]
>
> 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

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 `to` array when [sending a notification](/docs/push-notifications/api/notification-resource#send-a-pushnotification).
* **Register the device**: Store the token as a [DeviceRegistration](/docs/push-notifications/api/device-registration-resource#register-a-device) so you can send to Users by `userId`.

## Next steps

* [Send a push notification](/docs/push-notifications/send-a-push) using the token.
* [Obtain APNs credentials](/docs/push-notifications/apn-credentials) to upload to Twilio.
* Review Apple's [Registering your app with APNs](https://developer.apple.com/documentation/usernotifications/registering-your-app-with-apns) guide for additional details.
