Skip to content

Commit 9f4c2a2

Browse files
authored
Update README.md
1 parent c0fd17e commit 9f4c2a2

File tree

1 file changed

+30
-91
lines changed

1 file changed

+30
-91
lines changed

README.md

Lines changed: 30 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[![sswg:sandbox|94x20](https://img.shields.io/badge/sswg-sandbox-lightgrey.svg)](https://github.com/swift-server/sswg/blob/master/process/incubation.md#sandbox-level)
22
[![License](https://img.shields.io/badge/License-Apache%202.0-yellow.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)
33
[![Build](https://github.com/kylebrowning/APNSwift/workflows/test/badge.svg)](https://github.com/kylebrowning/APNSwift/actions)
4-
[![Swift](https://img.shields.io/badge/Swift-5.1-brightgreen.svg?colorA=orange&colorB=4E4E4E)](https://swift.org)
4+
[![Swift](https://img.shields.io/badge/Swift-5.2-brightgreen.svg?colorA=orange&colorB=4E4E4E)](https://swift.org)
55

66
# APNSwift
77

@@ -24,8 +24,20 @@ struct BasicNotification: APNSwiftNotification {
2424
let aps: APNSwiftPayload
2525
}
2626
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
27-
let signer = try! APNSwiftSigner(filePath: "/Users/kylebrowning/Downloads/AuthKey_9UC9ZLQ8YW.p8")
28-
let apnsConn = try APNSwiftConnection.connect(configuration: .init(keyIdentifier: "9UC9ZLQ8YW", teamIdentifier: "ABBM6U9RM5", signer: signer, topic: "com.grasscove.Fern", environment: .sandbox), on: group.next()).wait()
27+
var logger = Logger(label: "com.apnswift")
28+
logger.logLevel = .debug
29+
let apnsConfig = try APNSwiftConfiguration(
30+
authenticationMethod: .jwt(
31+
key: .private(filePath: "/Users/kylebrowning/Desktop/AuthKey_9UC9ZLQ8YW.p8"),
32+
keyIdentifier: "9UC9ZLQ8YW",
33+
teamIdentifier: "ABBM6U9RM5"
34+
),
35+
topic: "com.grasscove.Fern",
36+
environment: .sandbox,
37+
logger: logger
38+
)
39+
40+
let apns = try APNSwiftConnection.connect(configuration: apnsConfig, on: group.next()).wait()
2941
let aps = APNSwiftPayload(alert: .init(title: "Hey There", subtitle: "Subtitle", body: "Body"), hasContentAvailable: true)
3042
try apnsConn.send(BasicNotification(aps: aps), pushType: .alert, to: "98AAD4A2398DDC58595F02FA307DF9A15C18B6111D1B806949549085A8E6A55D").wait()
3143
try apns.close().wait()
@@ -38,22 +50,16 @@ exit(0)
3850
[`APNSwiftConfiguration`](https://github.com/kylebrowning/swift-nio-http2-apns/blob/master/Sources/APNSwift/APNSwiftConfiguration.swift) is a structure that provides the system with common configuration.
3951

4052
```swift
41-
public struct APNSwiftConfiguration {
42-
public var keyIdentifier: String
43-
public var teamIdentifier: String
44-
public var signer: APNSwiftSigner
45-
public var topic: String
46-
public var environment: Environment
47-
public var tlsConfiguration: TLSConfiguration
48-
49-
public var url: URL {
50-
switch environment {
51-
case .production:
52-
return URL(string: "https://api.push.apple.com")!
53-
case .sandbox:
54-
return URL(string: "https://api.development.push.apple.com")!
55-
}
56-
}
53+
let apnsConfig = try APNSwiftConfiguration(
54+
authenticationMethod: .jwt(
55+
key: .private(filePath: "/Users/kylebrowning/Desktop/AuthKey_9UC9ZLQ8YW.p8"),
56+
keyIdentifier: "9UC9ZLQ8YW",
57+
teamIdentifier: "ABBM6U9RM5"
58+
),
59+
topic: "com.grasscove.Fern",
60+
environment: .sandbox,
61+
logger: logger
62+
)
5763
```
5864
#### Example `APNSwiftConfiguration`
5965
```swift
@@ -65,22 +71,6 @@ let apnsConfig = try APNSwiftConfiguration(keyIdentifier: "9UC9ZLQ8YW",
6571
environment: .sandbox)
6672
```
6773

68-
### Signer
69-
70-
[`APNSwiftSigner`](https://github.com/kylebrowning/swift-nio-http2-apns/blob/master/Sources/APNSwift/APNSwiftSigner.swift) provides a structure to sign the payloads with. This should be loaded into memory at the configuration level. It requires the data to be in a ByteBuffer format.
71-
72-
```swift
73-
let url = URL(fileURLWithPath: "/Users/kylebrowning/Downloads/AuthKey_9UC9ZLQ8YW.p8")
74-
let data: Data
75-
do {
76-
data = try Data(contentsOf: url)
77-
} catch {
78-
throw APNSwiftError.SigningError.certificateFileDoesNotExist
79-
}
80-
var byteBuffer = ByteBufferAllocator().buffer(capacity: data.count)
81-
byteBuffer.writeBytes(data)
82-
let signer = try! APNSwiftSigner.init(buffer: byteBuffer)
83-
```
8474
### APNSwiftConnection
8575

8676
[`APNSwiftConnection`](https://github.com/kylebrowning/swift-nio-http2-apns/blob/master/Sources/APNSwift/APNSwiftConnection.swift) is a class with methods thats provides a wrapper to NIO's ClientBootstrap. The `swift-nio-http2` dependency is utilized here. It also provides a function to send a notification to a specific device token string.
@@ -113,60 +103,6 @@ let alert = ...
113103
let aps = APNSwiftPayload(alert: alert, badge: 1, sound: .normal("cow.wav"))
114104
```
115105

116-
## Putting it all together
117-
118-
```swift
119-
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
120-
var verbose = true
121-
122-
let signer = try! APNSwiftSigner(filePath: "/Users/kylebrowning/Desktop/AuthKey_9UC9ZLQ8YW.p8")
123-
124-
let apnsConfig = APNSwiftConfiguration(keyIdentifier: "9UC9ZLQ8YW",
125-
teamIdentifier: "ABBM6U9RM5",
126-
signer: signer,
127-
topic: "com.grasscove.Fern",
128-
environment: .sandbox)
129-
130-
let apns = try APNSwiftConnection.connect(configuration: apnsConfig, on: group.next()).wait()
131-
132-
if verbose {
133-
print("* Connected to \(apnsConfig.url.host!) (\(apns.channel.remoteAddress!)")
134-
}
135-
136-
struct AcmeNotification: APNSwiftNotification {
137-
let acme2: [String]
138-
let aps: APNSwiftPayload
139-
140-
init(acme2: [String], aps: APNSwiftPayload) {
141-
self.acme2 = acme2
142-
self.aps = aps
143-
}
144-
}
145-
146-
let alert = APNSwiftPayload.APNSwiftAlert(title: "Hey There", subtitle: "Subtitle", body: "Body")
147-
let apsSound = APNSwiftPayload.APNSSoundDictionary(isCritical: true, name: "cow.wav", volume: 0.8)
148-
let aps = APNSwiftPayload(alert: alert, badge: 0, sound: .critical(apsSound), hasContentAvailable: true)
149-
let temp = try! JSONEncoder().encode(aps)
150-
let string = String(bytes: temp, encoding: .utf8)
151-
let notification = AcmeNotification(acme2: ["bang", "whiz"], aps: aps)
152-
153-
do {
154-
let expiry = Date().addingTimeInterval(5)
155-
for _ in 1...5 {
156-
try apns.send(notification, pushType: .alert, to: "98AAD4A2398DDC58595F02FA307DF9A15C18B6111D1B806949549085A8E6A55D", expiration: expiry, priority: 10).wait()
157-
try apns.send(notification, pushType: .alert, to: "98AAD4A2398DDC58595F02FA307DF9A15C18B6111D1B806949549085A8E6A55D", expiration: expiry, priority: 10).wait()
158-
try apns.send(notification, pushType: .alert, to: "98AAD4A2398DDC58595F02FA307DF9A15C18B6111D1B806949549085A8E6A55D", expiration: expiry, priority: 10).wait()
159-
try apns.send(notification, pushType: .alert, to: "98AAD4A2398DDC58595F02FA307DF9A15C18B6111D1B806949549085A8E6A55D", expiration: expiry, priority: 10).wait()
160-
}
161-
} catch {
162-
print(error)
163-
}
164-
165-
try apns.close().wait()
166-
try group.syncShutdownGracefully()
167-
exit(0)
168-
```
169-
170106
### Custom Notification Data
171107

172108
Apple provides engineers with the ability to add custom payload data to each notification. In order to facilitate this we have the `APNSwiftNotification`.
@@ -192,9 +128,12 @@ let res = try apns.send(notification, to: "de1d666223de85db0186f654852cc96055112
192128
### Using PEM instead of P8
193129
#### Note: this is blocking
194130
```swift
131+
195132
var apnsConfig = try APNSwiftConfiguration(
196-
privateKeyPath: "/Users/kylebrowning/Projects/swift/Fern/development_com.grasscove.Fern.pkey",
197-
pemPath: "/Users/kylebrowning/Projects/swift/Fern/development_com.grasscove.Fern.pem",
133+
authenticationMethod: .tls(
134+
privateKeyPath: "/Users/kylebrowning/Projects/swift/Fern/development_com.grasscove.Fern.pkey",
135+
pemPath: "/Users/kylebrowning/Projects/swift/Fern/development_com.grasscove.Fern.pem"
136+
),
198137
topic: "com.grasscove.Fern",
199138
environment: .sandbox
200139
)

0 commit comments

Comments
 (0)