Swiftpack.co - swift-server-community/APNSwift as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by swift-server-community.
swift-server-community/APNSwift 5.0.0-beta.1
An HTTP/2 APNS library built on AsyncHTTPClient
⭐️ 468
🕓 4 weeks ago
iOS macOS
.package(url: "https://github.com/swift-server-community/APNSwift.git", from: "5.0.0-beta.1")

sswg:incubating|94x20 Build Documentation


A non-blocking Swift module for sending remote Apple Push Notification requests to APNS built on AsyncHttpClient.


To install APNSwift, just add the package as a dependency in your Package.swift.

dependencies: [
    .package(url: "https://github.com/swift-server-community/APNSwift.git", from: "4.0.0"),

If youd like to give our bleeding edge release a try, which is what the Readme is expecting use 5.0.0-alpha.N. If you need the old Readme, see here

dependencies: [
    .package(url: "https://github.com/swift-server-community/APNSwift.git", from: "5.0.0-alpha.5"),


APNSwift is built with a layered approach. It exposes three tiers of API's.

  1. A raw API that takes basic types such as String's
  2. A slightly more semantically safe API, which takes types, like APNSPriority, APNSPushType, APNSNotificationExpiration, etc.
  3. The safest API which takes fully semantic types such as APNSAlertNotification

We recommened using number 3, the semantically safest API to ensure your push notification is delivered correctly. This README assumes that you are using number 3. However if you need more granular approach, or something doesn't exist in this library, please use 2 or 1. (Also please open an issue if we missed something so we can get a semantically correct version!)

Getting Started

APNSwift aims to provide sementically correct structures to sending push notifications. You first need to setup a APNSClient. To do that youll need to know your authentication method

let client = APNSClient(
    configuration: .init(
        authenticationMethod: .jwt(
            privateKey: try .init(pemRepresentation: privateKey),
            keyIdentifier: keyIdentifier,
            teamIdentifier: teamIdentifier
        environment: .sandbox
    eventLoopGroupProvider: .createNew,
    responseDecoder: JSONDecoder(),
    requestEncoder: JSONEncoder(),
    byteBufferAllocator: .init(),
    backgroundActivityLogger: logger
defer {
    client.shutdown { _ in
        logger.error("Failed to shutdown APNSClient")

Sending a simple notification

All notifications require a payload, but that payload can be empty. Payload just needs to conform to Encodable

struct Payload: Codable {}

try await client.sendAlertNotification(
        alert: .init(
            title: .raw("Simple Alert"),
            subtitle: .raw("Subtitle"),
            body: .raw("Body"),
            launchImage: nil
        expiration: .immediately,
        priority: .immediately,
        topic: "com.app.bundle",
        payload: Payload()
    deviceToken: "device-token",
    deadline: .distantFuture,
    logger: myLogger


APNSwift provides two authentication methods. jwt, and TLS.

jwt is preferred and recommend by Apple These can be configured when created your APNSClientConfiguration

Notes: jwt requires an encrypted version of your .p8 file from Apple which comes in a pem format. If you're having trouble with your key being invalid please confirm it is a PEM file

 openssl pkcs8 -nocrypt -in /path/to/my/key.p8 -out ~/Downloads/key.pem


By default APNSwift has a no-op logger which will not log anything. However if you pass a logger in, you will see logs.

There are currently two kinds of loggers.

Background Activity Logger

This logger can be passed into the APNSClient and will log background things like connection pooling, auth token refreshes, etc.

Notification Send Logger

This logger can be passed into any of the send: methods and will log everything related to a single send request.

Using the non semantic safe APIs

APNSwift provides the ability to send raw payloads. You can use Data, ByteBuffer, DispatchData, Array Though this is to be used with caution. APNSwift cannot gurantee delivery if you do not have the correct payload. For more information see: Creating APN Payload

/// Extremely Raw,
try await client.send(
    payload: payload, 
    deviceToken: token, 
    pushType: "alert", deadline: .distantFuture

/// or a little safer but still raw
try await client.send(
    payload: payload, 
    deviceToken: token, 
    pushType: .alert, 
    expiration: .immediatly, 
    priority: .immediatly, 
    deadline: .distantFuture

Server Example

Take a look at Program.swift

iOS Examples

For an iOS example, open the example project within this repo.

Once inside configure your App Bundle ID and assign your development team. Build and run the ExampleApp to iOS Simulator, grab your device token, and plug it in to server example above. Background the app and run Program.swift

Original pitch and discussion on API


Stars: 468
Last commit: 4 days ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Related Packages

Release Notes

4 weeks ago

What's Changed

New Contributors

Full Changelog: https://github.com/swift-server-community/APNSwift/compare/4.0.0...5.0.0-beta.1

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics