Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,17 @@ apnsConfig.tlsConfiguration.privateKey = NIOSSLPrivateKeySource.privateKey(key)
apnsConfig.tlsConfiguration.certificateVerification = .noHostnameVerification
apnsConfig.tlsConfiguration.certificateChain = try! [.certificate(.init(file: "/Users/kylebrowning/Projects/swift/Fern/development_com.grasscove.Fern.pem", format: .pem))]
```
### Need a completely custom arbtirary payload and dont like being typecast?
APNSwift provides the ability to send rawBytes `ByteBuffer` as a payload.
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](https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html)
```swift
let notificationJsonPayload = ...
let data: Data = try! encoder.encode(notificationJsonPayload)
var buffer = ByteBufferAllocator().buffer(capacity: data.count)
buffer.writeBytes(data)
try apns.send(rawBytes: buffer, pushType: .alert, to: "<DEVICETOKEN>")
```

#### Original pitch and discussion on API

Expand Down
15 changes: 10 additions & 5 deletions Sources/APNSwift/APNSwiftConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ public final class APNSwiftConnection {
```
*/
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> {
let data: Data = try! encoder.encode(notification)
var buffer = ByteBufferAllocator().buffer(capacity: data.count)
buffer.writeBytes(data)
return send(rawBytes: buffer, pushType: pushType, to: deviceToken)
}

/// 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](https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html)
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> {
let streamPromise = channel.eventLoop.makePromise(of: Channel.self)
multiplexer.createStreamChannel(promise: streamPromise) { channel, streamID in
let handlers: [ChannelHandler] = [
Expand All @@ -166,12 +175,8 @@ public final class APNSwiftConnection {
}

let responsePromise = channel.eventLoop.makePromise(of: Void.self)
let data: Data = try! encoder.encode(notification)

var buffer = ByteBufferAllocator().buffer(capacity: data.count)
buffer.writeBytes(data)
let context = APNSwiftRequestContext(
request: buffer,
request: payload,
responsePromise: responsePromise
)

Expand Down