Skip to content

Commit

Permalink
custom logging + APNSwiftClient (#74)
Browse files Browse the repository at this point in the history
* custom logging + APNSwiftClient

* remove extraneous file

* add return keyword

* One more return stagement

* add old signature, call new one.

* Adds mising return.

* Update to use raw

* Moves around to use new send Raw.

* fix duplicate methods

* cleanup APNSwift protocol

* Remove custom logger.
  • Loading branch information
tanner0101 authored and kylebrowning committed Dec 9, 2019
1 parent 2d4a461 commit b5ca1c9
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 72 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,13 @@ var apnsConfig = try APNSwiftConfiguration(
let apns = try APNSwiftConnection.connect(configuration: apnsConfig, on: group.next()).wait()
```
### 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.
APNSwift provides the ability to send raw payloads. You can use `Data`, `ByteBuffer`, `DispatchData`, `Array`
Though 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>")
try apns.send(raw: data, pushType: .alert, to: "<DEVICETOKEN>")
```

#### Original pitch and discussion on API
Expand Down
191 changes: 191 additions & 0 deletions Sources/APNSwift/APNSwiftClient.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
import Foundation
import Logging
import NIO

public protocol APNSwiftClient {
var logger: Logger? { get }
var eventLoop: EventLoop { get }

func send(rawBytes payload: ByteBuffer,
pushType: APNSwiftConnection.PushType,
to deviceToken: String,
expiration: Date?,
priority: Int?,
collapseIdentifier: String?,
topic: String?,
logger: Logger?) -> EventLoopFuture<Void>
}

extension APNSwiftClient {
/**
APNSwiftConnection send method. Sends a notification to the desired deviceToken.
- Parameter payload: the alert to send.
- Parameter pushType: push type of the notification.
- Parameter deviceToken: device token to send alert to.
- Parameter encoder: customer JSON encoder if needed.
- Parameter expiration: a date that the notificaiton expires.
- Parameter priority: priority to send the notification with.
- Parameter collapseIdentifier: a collapse identifier to use for grouping notifications
- Parameter topic: the bundle identifier that this notification belongs to.

For more information see:
[Retrieve Your App's Device Token](https://developer.apple.com/documentation/usernotifications/registering_your_app_with_apns#2942135)
### Usage Example: ###
```
let apns = APNSwiftConnection.connect()
let expiry = Date().addingTimeInterval(5)
try apns.send(notification, pushType: .alert, to: "b27a07be2092c7fbb02ab5f62f3135c615e18acc0ddf39a30ffde34d41665276", with: JSONEncoder(), expiration: expiry, priority: 10, collapseIdentifier: "huro2").wait()
```
*/
public func send(_ alert: APNSwiftPayload.APNSwiftAlert,
pushType: APNSwiftConnection.PushType = .alert,
to deviceToken: String,
with encoder: JSONEncoder = JSONEncoder(),
expiration: Date? = nil,
priority: Int? = nil,
collapseIdentifier: String? = nil,
topic: String? = nil,
logger: Logger? = nil) -> EventLoopFuture<Void> {
return self.send(APNSwiftPayload(alert: alert),
pushType: pushType,
to: deviceToken,
with: encoder,
expiration: expiration,
priority: priority,
collapseIdentifier: collapseIdentifier,
topic: topic,
logger: logger ?? self.logger)
}

/**
APNSwiftConnection send method. Sends a notification to the desired deviceToken.
- Parameter payload: the payload to send.
- Parameter pushType: push type of the notification.
- Parameter deviceToken: device token to send alert to.
- Parameter encoder: customer JSON encoder if needed.
- Parameter expiration: a date that the notificaiton expires.
- Parameter priority: priority to send the notification with.
- Parameter collapseIdentifier: a collapse identifier to use for grouping notifications
- Parameter topic: the bundle identifier that this notification belongs to.

For more information see:
[Retrieve Your App's Device Token](https://developer.apple.com/documentation/usernotifications/registering_your_app_with_apns#2942135)
### Usage Example: ###
```
let apns = APNSwiftConnection.connect()
let expiry = Date().addingTimeInterval(5)
try apns.send(notification, pushType: .alert, to: "b27a07be2092c7fbb02ab5f62f3135c615e18acc0ddf39a30ffde34d41665276", with: JSONEncoder(), expiration: expiry, priority: 10, collapseIdentifier: "huro2").wait()
```
*/
public func send(_ payload: APNSwiftPayload,
pushType: APNSwiftConnection.PushType = .alert,
to deviceToken: String,
with encoder: JSONEncoder = JSONEncoder(),
expiration: Date? = nil,
priority: Int? = nil,
collapseIdentifier: String? = nil,
topic: String? = nil,
logger: Logger? = nil) -> EventLoopFuture<Void> {
return self.send(BasicNotification(aps: payload),
pushType: pushType,
to: deviceToken,
with: encoder,
expiration: expiration,
priority: priority,
collapseIdentifier: collapseIdentifier,
topic: topic,
logger: logger ?? self.logger)
}

/**
APNSwiftConnection send method. Sends a notification to the desired deviceToken.
- Parameter notification: the notification meta data and alert to send.
- Parameter pushType: push type of the notification.
- Parameter deviceToken: device token to send alert to.
- Parameter encoder: customer JSON encoder if needed.
- Parameter expiration: a date that the notificaiton expires.
- Parameter priority: priority to send the notification with.
- Parameter collapseIdentifier: a collapse identifier to use for grouping notifications
- Parameter topic: the bundle identifier that this notification belongs to.

For more information see:
[Retrieve Your App's Device Token](https://developer.apple.com/documentation/usernotifications/registering_your_app_with_apns#2942135)
### Usage Example: ###
```
let apns = APNSwiftConnection.connect()
let expiry = Date().addingTimeInterval(5)
try apns.send(notification, pushType: .alert, to: "b27a07be2092c7fbb02ab5f62f3135c615e18acc0ddf39a30ffde34d41665276", with: JSONEncoder(), expiration: expiry, priority: 10, collapseIdentifier: "huro2").wait()
```
*/
public func send<Notification>(_ notification: Notification,
pushType: APNSwiftConnection.PushType = .alert,
to deviceToken: String,
with encoder: JSONEncoder = JSONEncoder(),
expiration: Date? = nil,
priority: Int? = nil,
collapseIdentifier: String? = nil,
topic: String? = nil,
logger: Logger? = nil) -> EventLoopFuture<Void>
where Notification: APNSwiftNotification {
do {
let data: Data = try encoder.encode(notification)
return self.send(raw: data,
pushType: pushType,
to: deviceToken,
expiration: expiration,
priority: priority,
collapseIdentifier: collapseIdentifier,
topic: topic,
logger: logger ?? self.logger)
} catch {
return self.eventLoop.makeFailedFuture(error)
}
}

/// 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<Bytes>(raw payload: Bytes,
pushType: APNSwiftConnection.PushType = .alert,
to deviceToken: String,
expiration: Date?,
priority: Int?,
collapseIdentifier: String?,
topic: String?,
logger: Logger? = nil) -> EventLoopFuture<Void>
where Bytes : Collection, Bytes.Element == UInt8 {
var buffer = ByteBufferAllocator().buffer(capacity: payload.count)
buffer.writeBytes(payload)
return self.send(rawBytes: buffer,
pushType: pushType,
to: deviceToken,
expiration: expiration,
priority: priority,
collapseIdentifier: collapseIdentifier,
topic: topic,
logger: logger ?? self.logger)
}

public func send(rawBytes payload: ByteBuffer,
pushType: APNSwiftConnection.PushType = .alert,
to deviceToken: String,
expiration: Date? = nil,
priority: Int? = nil,
collapseIdentifier: String? = nil,
topic: String? = nil,
logger: Logger? = nil) -> EventLoopFuture<Void> {
return self.send(
rawBytes: payload,
pushType: pushType,
to: deviceToken,
expiration: expiration,
priority: priority,
collapseIdentifier: collapseIdentifier,
topic: topic,
logger: logger ?? self.logger
)
}
}

private struct BasicNotification: APNSwiftNotification {
let aps: APNSwiftPayload
}
2 changes: 1 addition & 1 deletion Sources/APNSwift/APNSwiftConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public struct APNSwiftConfiguration {
)
)
```
*/
*/
public init(privateKeyPath: String, pemPath: String, topic: String, environment: APNSwiftConfiguration.Environment, logger: Logger? = nil) throws {
try self.init(keyIdentifier: "", teamIdentifier: "", signer: APNSwiftSigner(buffer: ByteBufferAllocator().buffer(capacity: 1024)), topic: topic, environment: environment, logger: logger)
let key = try NIOSSLPrivateKey(file: privateKeyPath, format: .pem)
Expand Down
Loading

0 comments on commit b5ca1c9

Please sign in to comment.