Skip to content

Commit f9088ef

Browse files
authored
Update README.md
1 parent 96d694b commit f9088ef

File tree

1 file changed

+60
-18
lines changed

1 file changed

+60
-18
lines changed

README.md

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ A non-blocking Swift module for sending remote Apple Push Notification requests
99

1010
## Installation
1111

12-
To install `APNSwift`, just add the package as a dependency in your [**Package.swift**](https://github.com/apple/swift-package-manager/blob/master/Documentation/PackageDescriptionV4.md#dependencies). Though you may want to consider using master branch until approved by the SSWG.
12+
To install `APNSwift`, just add the package as a dependency in your [**Package.swift**](https://github.com/apple/swift-package-manager/blob/master/Documentation/PackageDescriptionV4.md#dependencies).
1313

1414
```swift
1515
dependencies: [
@@ -28,13 +28,17 @@ let apnsConfig = APNSwiftConfiguration(keyIdentifier: "9UC9ZLQ8YW",
2828
topic: "com.grasscove.Fern",
2929
environment: .sandbox)
3030

31+
struct BasicNotification: APNSwiftNotification {
32+
var aps: APNSwiftPayload
33+
}
3134
let apns = try APNSwiftConnection.connect(configuration: apnsConfig, on: group.next()).wait()
32-
let alert = Alert(title: "Hey There", subtitle: "Full moon sighting", body: "There was a full moon last night did you see it")
35+
let alert = APNSwiftPayload.APNSwiftAlert(title: "Hey There", subtitle: "Full moon sighting", body: "There was a full moon last night did you see it")
3336
let aps = APNSwiftPayload(alert: alert, badge: 1, sound: .normal("cow.wav"))
34-
let notification = APNSwiftNotification(aps: aps)
35-
let res = try apns.send(notification, to: "de1d666223de85db0186f654852cc960551125ee841ca044fdf5ef6a4756a77e").wait()
37+
let notification = BasicNotification(aps: aps)
38+
let res = apns.send(notification, pushType: .alert, to: "de1d666223de85db0186f654852cc960551125ee841ca044fdf5ef6a4756a77e")
3639
try apns.close().wait()
3740
try group.syncShutdownGracefully()
41+
exit(0)
3842
```
3943

4044

@@ -122,16 +126,9 @@ let aps = APNSwiftPayload(alert: alert, badge: 1, sound: .normal("cow.wav"))
122126

123127
```swift
124128
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
125-
let url = URL(fileURLWithPath: "/Users/kylebrowning/Downloads/AuthKey_9UC9ZLQ8YW.p8")
126-
let data: Data
127-
do {
128-
data = try Data(contentsOf: url)
129-
} catch {
130-
throw APNSwiftError.SigningError.certificateFileDoesNotExist
131-
}
132-
var byteBuffer = ByteBufferAllocator().buffer(capacity: data.count)
133-
byteBuffer.writeBytes(data)
134-
let signer = try! APNSwiftSigner.init(buffer: byteBuffer)
129+
var verbose = true
130+
131+
let signer = try! APNSwiftSigner(filePath: "/Users/kylebrowning/Desktop/AuthKey_9UC9ZLQ8YW.p8")
135132

136133
let apnsConfig = APNSwiftConfiguration(keyIdentifier: "9UC9ZLQ8YW",
137134
teamIdentifier: "ABBM6U9RM5",
@@ -140,12 +137,43 @@ let apnsConfig = APNSwiftConfiguration(keyIdentifier: "9UC9ZLQ8YW",
140137
environment: .sandbox)
141138

142139
let apns = try APNSwiftConnection.connect(configuration: apnsConfig, on: group.next()).wait()
143-
let alert = Alert(title: "Hey There", subtitle: "Full moon sighting", body: "There was a full moon last night did you see it")
144-
let aps = APNSwiftPayload(alert: alert, badge: 1, sound: .normal("cow.wav"))
145-
let notification = BasicNotification(aps: aps)
146-
let res = try apns.send(notification, to: "de1d666223de85db0186f654852cc960551125ee841ca044fdf5ef6a4756a77e").wait()
140+
141+
if verbose {
142+
print("* Connected to \(apnsConfig.url.host!) (\(apns.channel.remoteAddress!)")
143+
}
144+
145+
struct AcmeNotification: APNSwiftNotification {
146+
let acme2: [String]
147+
let aps: APNSwiftPayload
148+
149+
init(acme2: [String], aps: APNSwiftPayload) {
150+
self.acme2 = acme2
151+
self.aps = aps
152+
}
153+
}
154+
155+
let alert = APNSwiftPayload.APNSwiftAlert(title: "Hey There", subtitle: "Subtitle", body: "Body")
156+
let apsSound = APNSwiftPayload.APNSSoundDictionary(isCritical: true, name: "cow.wav", volume: 0.8)
157+
let aps = APNSwiftPayload(alert: alert, badge: 0, sound: .critical(apsSound), hasContentAvailable: true)
158+
let temp = try! JSONEncoder().encode(aps)
159+
let string = String(bytes: temp, encoding: .utf8)
160+
let notification = AcmeNotification(acme2: ["bang", "whiz"], aps: aps)
161+
162+
do {
163+
let expiry = Date().addingTimeInterval(5)
164+
for _ in 1...5 {
165+
try apns.send(notification, pushType: .alert, to: "98AAD4A2398DDC58595F02FA307DF9A15C18B6111D1B806949549085A8E6A55D", expiration: expiry, priority: 10).wait()
166+
try apns.send(notification, pushType: .alert, to: "98AAD4A2398DDC58595F02FA307DF9A15C18B6111D1B806949549085A8E6A55D", expiration: expiry, priority: 10).wait()
167+
try apns.send(notification, pushType: .alert, to: "98AAD4A2398DDC58595F02FA307DF9A15C18B6111D1B806949549085A8E6A55D", expiration: expiry, priority: 10).wait()
168+
try apns.send(notification, pushType: .alert, to: "98AAD4A2398DDC58595F02FA307DF9A15C18B6111D1B806949549085A8E6A55D", expiration: expiry, priority: 10).wait()
169+
}
170+
} catch {
171+
print(error)
172+
}
173+
147174
try apns.close().wait()
148175
try group.syncShutdownGracefully()
176+
exit(0)
149177
```
150178

151179
### Custom Notification Data
@@ -170,6 +198,20 @@ let notification = AcmeNotification(acme2: ["bang", "whiz"], aps: aps)
170198
let res = try apns.send(notification, to: "de1d666223de85db0186f654852cc960551125ee841ca044fdf5ef6a4756a77e").wait()
171199
```
172200

201+
### Using PEM instead of P8
202+
```swift
203+
var apnsConfig = try APNSwiftConfiguration(keyIdentifier: "9UC9ZLQ8YW",
204+
teamIdentifier: "ABBM6U9RM5",
205+
signer: APNSwiftSigner.init(buffer: ByteBufferAllocator().buffer(capacity: Data().count)),
206+
topic: "com.grasscove.Fern",
207+
environment: .sandbox)
208+
209+
let key = try NIOSSLPrivateKey(file: "/Users/kylebrowning/Projects/swift/Fern/development_com.grasscove.Fern.pkey", format: .pem)
210+
apnsConfig.tlsConfiguration.privateKey = NIOSSLPrivateKeySource.privateKey(key)
211+
apnsConfig.tlsConfiguration.certificateVerification = .noHostnameVerification
212+
apnsConfig.tlsConfiguration.certificateChain = try! [.certificate(.init(file: "/Users/kylebrowning/Projects/swift/Fern/development_com.grasscove.Fern.pem", format: .pem))]
213+
```
214+
173215
#### Original pitch and discussion on API
174216

175217
* Pitch discussion: [Swift Server Forums](https://forums.swift.org/t/apple-push-notification-service-implementation-pitch/20193)

0 commit comments

Comments
 (0)