Skip to content

polyitan/SwiftyAPNS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SwiftyAPNS

HTTP/2 Apple Push Notification Service for Swift.

There are two ways how the provider server can communicate with the Apple Push Notification service (APNs). The framework is supported both token-based and certificate-based authentication, but the former is preferred.

JWT Token provider

Before sending any pushes need to instantiate the APNs provider. For provider with a token-based connection need to know Apple Developer Team ID (can be getting from the Account page) and Apple Push Notification Authentication Key.

let keyP8 = <#Apple Auth Key (.p8) content#>
let keyId = <#Apple Auth Key ID#>
let teamId = <#Apple Developer Team ID#>
let provider = APNSProvider.init(p8: keyP8, keyId: keyId, teamId: teamId)

Payload examples

Plain payload:

let plain = APNSPayload(alert: .plain(plain: "Plain notification."))

Localized payload:

var alert = APSLocalizedAlert()
alert.title = "Title"
alert.subtitle = "Subtitle"
alert.body = "Localized notification body."
let localized = APNSPayload(alert: .localized(alert: alert))

Notification example

To create Notification we need a DeviceToken, a Topic, and a Payload.

var options = APNSNotificationOptions.default
options.type = .alert
options.topic = <#Remote Notification Topic#
let notification = APNSNotification.init(payload: <#Notification Payload#>,
                                         token: <#Device Token#>,
                                         options: options)

Sending push notifications

The values in responses can be handled asynchronously.

    do {
        let responce = try await provider.push(notification)
        // Push Notification was successfully sent
    } catch {
        // Push Notification failure
    }

License

MIT License