Skip to content

Commit a4884ba

Browse files
authored
Support raw payloads. (#51)
* Suport raw payloads. * Addressing feedback. * Update Readme.
1 parent f9088ef commit a4884ba

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,17 @@ apnsConfig.tlsConfiguration.privateKey = NIOSSLPrivateKeySource.privateKey(key)
211211
apnsConfig.tlsConfiguration.certificateVerification = .noHostnameVerification
212212
apnsConfig.tlsConfiguration.certificateChain = try! [.certificate(.init(file: "/Users/kylebrowning/Projects/swift/Fern/development_com.grasscove.Fern.pem", format: .pem))]
213213
```
214+
### Need a completely custom arbtirary payload and dont like being typecast?
215+
APNSwift provides the ability to send rawBytes `ByteBuffer` as a payload.
216+
This is to be used with caution. APNSwift cannot gurantee delivery if you do not have the correct payload.
217+
For more information see: [Creating APN Payload](https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html)
218+
```swift
219+
let notificationJsonPayload = ...
220+
let data: Data = try! encoder.encode(notificationJsonPayload)
221+
var buffer = ByteBufferAllocator().buffer(capacity: data.count)
222+
buffer.writeBytes(data)
223+
try apns.send(rawBytes: buffer, pushType: .alert, to: "<DEVICETOKEN>")
224+
```
214225

215226
#### Original pitch and discussion on API
216227

Sources/APNSwift/APNSwiftConnection.swift

+10-5
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ public final class APNSwiftConnection {
154154
```
155155
*/
156156
public func send<Notification: APNSwiftNotification>(_ notification: Notification, pushType: APNSwiftConnection.PushType, to deviceToken: String, with encoder: JSONEncoder = JSONEncoder(), expiration: Date? = nil, priority: Int? = nil, collapseIdentifier: String? = nil, topic: String? = nil) -> EventLoopFuture<Void> {
157+
let data: Data = try! encoder.encode(notification)
158+
var buffer = ByteBufferAllocator().buffer(capacity: data.count)
159+
buffer.writeBytes(data)
160+
return send(rawBytes: buffer, pushType: pushType, to: deviceToken)
161+
}
162+
163+
/// This is to be used with caution. APNSwift cannot gurantee delivery if you do not have the correct payload.
164+
/// For more information see: [Creating APN Payload](https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html)
165+
public func send(rawBytes payload: ByteBuffer, pushType: APNSwiftConnection.PushType, to deviceToken: String, expiration: Date? = nil, priority: Int? = nil, collapseIdentifier: String? = nil, topic: String? = nil) -> EventLoopFuture<Void> {
157166
let streamPromise = channel.eventLoop.makePromise(of: Channel.self)
158167
multiplexer.createStreamChannel(promise: streamPromise) { channel, streamID in
159168
let handlers: [ChannelHandler] = [
@@ -166,12 +175,8 @@ public final class APNSwiftConnection {
166175
}
167176

168177
let responsePromise = channel.eventLoop.makePromise(of: Void.self)
169-
let data: Data = try! encoder.encode(notification)
170-
171-
var buffer = ByteBufferAllocator().buffer(capacity: data.count)
172-
buffer.writeBytes(data)
173178
let context = APNSwiftRequestContext(
174-
request: buffer,
179+
request: payload,
175180
responsePromise: responsePromise
176181
)
177182

0 commit comments

Comments
 (0)