swift-server-community/APNSwift

Add environment when sending notification.

Andrewangeta opened this issue · 2 comments

Is your feature request related to a problem? Please describe.
When developing backend APIs in multiple environments, there are certain scenarios where using a TestFlight build (which requires the production APNS environment) can mean server code is either in development or production environment.

When setting up APNS with Vapor for example it can be handy to set the configuration's environment based on the Vapor server environment:

try app.apns.configuration = .init(authenticationMethod: .jwt(key: .private(pem: pem),
                                                              keyIdentifier: .init(string: keyId),
                                                              teamIdentifier: teamId),
                                   topic: "",
                                   environment: app.environment == .production ? .production : .sandbox,
                                   logger: app.logger,
                                   timeout: .seconds(2))

Describe the solution you'd like
Ideally being able to pass the environment at the call sight based on business logic would be helpful.
The same way we can specify the topic at the call site.

for token in iosTokens {
            do {
                try await apns.send(notification,
                                    pushType: .alert,
                                    to: token.token,
                                    topic: "com.my.app",
                                    environment: token.isTestFlight ? .production : .sandbox, // <--- New addition potentially. 
                                    logger: logger)
            } catch {
                logger.error("Failed to send APNS notification. \(error.localizedDescription) with token '\(token.token)'.")
            }
        }

Describe alternatives you've considered
Mutating the configuration at the call site.

apns.configuration?.environment = token.isTestFlight ? .production : .sandbox

This is fixed in #130 but that is a WIP for now

Fixed!