Skip to content

Instance Method

configure(_:)

Configure both a production and development APNs environment.
func configure(_ authenticationMethod: APNSClientConfiguration.AuthenticationMethod) async

Parameters

authenticationMethod

An APNs authentication method to use when connecting to Apple’s production and development servers.

Discussion

This convenience method creates two clients available via client(_:) with production and development that make it easy to support both development builds (ie. run from Xcode) and release builds (ie. TestFlight/App Store):

/// The .p8 file as a string.
guard let apnsKey = Environment.get("APNS_KEY_P8")
else { throw Abort(.serviceUnavailable) }

app.apns.configure(.jwt(
    privateKey: try .loadFrom(string: apnsKey),
    /// The identifier of the key in the developer portal.
    keyIdentifier: Environment.get("APNS_KEY_ID"),
    /// The team identifier of the app in the developer portal.
    teamIdentifier: Environment.get("APNS_TEAM_ID")
))

// ...

let response = switch deviceToken.environment {
case .production:
    try await apns.client(.production)
        .sendAlertNotification(notification, deviceToken: deviceToken.hexadecimalToken)
case .development:
    try await apns.client(.development)
        .sendAlertNotification(notification, deviceToken: deviceToken.hexadecimalToken)
}

For more control over configuration, including sample code to determine the environment an APFs device token belongs to, see use(_:eventLoopGroupProvider:responseDecoder:requestEncoder:byteBufferAllocator:as:isDefault:).

Note

The same key can be used for both the development and production environments.

Important

Make sure not to store your APNs key within your code or repo directly, and opt to store it via a secure store specific to your deployment, such as in a .env supplied at deploy time.