Skip to content

Commit b5ca1c9

Browse files
tanner0101kylebrowning
authored andcommitted
custom logging + APNSwiftClient (#74)
* 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.
1 parent 2d4a461 commit b5ca1c9

File tree

5 files changed

+269
-72
lines changed

5 files changed

+269
-72
lines changed

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,13 @@ var apnsConfig = try APNSwiftConfiguration(
211211
let apns = try APNSwiftConnection.connect(configuration: apnsConfig, on: group.next()).wait()
212212
```
213213
### Need a completely custom arbtirary payload and dont like being typecast?
214-
APNSwift provides the ability to send rawBytes `ByteBuffer` as a payload.
215-
This is to be used with caution. APNSwift cannot gurantee delivery if you do not have the correct payload.
214+
APNSwift provides the ability to send raw payloads. You can use `Data`, `ByteBuffer`, `DispatchData`, `Array`
215+
Though this is to be used with caution. APNSwift cannot gurantee delivery if you do not have the correct payload.
216216
For more information see: [Creating APN Payload](https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html)
217217
```swift
218218
let notificationJsonPayload = ...
219219
let data: Data = try! encoder.encode(notificationJsonPayload)
220-
var buffer = ByteBufferAllocator().buffer(capacity: data.count)
221-
buffer.writeBytes(data)
222-
try apns.send(rawBytes: buffer, pushType: .alert, to: "<DEVICETOKEN>")
220+
try apns.send(raw: data, pushType: .alert, to: "<DEVICETOKEN>")
223221
```
224222

225223
#### Original pitch and discussion on API
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
import Foundation
2+
import Logging
3+
import NIO
4+
5+
public protocol APNSwiftClient {
6+
var logger: Logger? { get }
7+
var eventLoop: EventLoop { get }
8+
9+
func send(rawBytes payload: ByteBuffer,
10+
pushType: APNSwiftConnection.PushType,
11+
to deviceToken: String,
12+
expiration: Date?,
13+
priority: Int?,
14+
collapseIdentifier: String?,
15+
topic: String?,
16+
logger: Logger?) -> EventLoopFuture<Void>
17+
}
18+
19+
extension APNSwiftClient {
20+
/**
21+
APNSwiftConnection send method. Sends a notification to the desired deviceToken.
22+
- Parameter payload: the alert to send.
23+
- Parameter pushType: push type of the notification.
24+
- Parameter deviceToken: device token to send alert to.
25+
- Parameter encoder: customer JSON encoder if needed.
26+
- Parameter expiration: a date that the notificaiton expires.
27+
- Parameter priority: priority to send the notification with.
28+
- Parameter collapseIdentifier: a collapse identifier to use for grouping notifications
29+
- Parameter topic: the bundle identifier that this notification belongs to.
30+
31+
For more information see:
32+
[Retrieve Your App's Device Token](https://developer.apple.com/documentation/usernotifications/registering_your_app_with_apns#2942135)
33+
### Usage Example: ###
34+
```
35+
let apns = APNSwiftConnection.connect()
36+
let expiry = Date().addingTimeInterval(5)
37+
try apns.send(notification, pushType: .alert, to: "b27a07be2092c7fbb02ab5f62f3135c615e18acc0ddf39a30ffde34d41665276", with: JSONEncoder(), expiration: expiry, priority: 10, collapseIdentifier: "huro2").wait()
38+
```
39+
*/
40+
public func send(_ alert: APNSwiftPayload.APNSwiftAlert,
41+
pushType: APNSwiftConnection.PushType = .alert,
42+
to deviceToken: String,
43+
with encoder: JSONEncoder = JSONEncoder(),
44+
expiration: Date? = nil,
45+
priority: Int? = nil,
46+
collapseIdentifier: String? = nil,
47+
topic: String? = nil,
48+
logger: Logger? = nil) -> EventLoopFuture<Void> {
49+
return self.send(APNSwiftPayload(alert: alert),
50+
pushType: pushType,
51+
to: deviceToken,
52+
with: encoder,
53+
expiration: expiration,
54+
priority: priority,
55+
collapseIdentifier: collapseIdentifier,
56+
topic: topic,
57+
logger: logger ?? self.logger)
58+
}
59+
60+
/**
61+
APNSwiftConnection send method. Sends a notification to the desired deviceToken.
62+
- Parameter payload: the payload to send.
63+
- Parameter pushType: push type of the notification.
64+
- Parameter deviceToken: device token to send alert to.
65+
- Parameter encoder: customer JSON encoder if needed.
66+
- Parameter expiration: a date that the notificaiton expires.
67+
- Parameter priority: priority to send the notification with.
68+
- Parameter collapseIdentifier: a collapse identifier to use for grouping notifications
69+
- Parameter topic: the bundle identifier that this notification belongs to.
70+
71+
For more information see:
72+
[Retrieve Your App's Device Token](https://developer.apple.com/documentation/usernotifications/registering_your_app_with_apns#2942135)
73+
### Usage Example: ###
74+
```
75+
let apns = APNSwiftConnection.connect()
76+
let expiry = Date().addingTimeInterval(5)
77+
try apns.send(notification, pushType: .alert, to: "b27a07be2092c7fbb02ab5f62f3135c615e18acc0ddf39a30ffde34d41665276", with: JSONEncoder(), expiration: expiry, priority: 10, collapseIdentifier: "huro2").wait()
78+
```
79+
*/
80+
public func send(_ payload: APNSwiftPayload,
81+
pushType: APNSwiftConnection.PushType = .alert,
82+
to deviceToken: String,
83+
with encoder: JSONEncoder = JSONEncoder(),
84+
expiration: Date? = nil,
85+
priority: Int? = nil,
86+
collapseIdentifier: String? = nil,
87+
topic: String? = nil,
88+
logger: Logger? = nil) -> EventLoopFuture<Void> {
89+
return self.send(BasicNotification(aps: payload),
90+
pushType: pushType,
91+
to: deviceToken,
92+
with: encoder,
93+
expiration: expiration,
94+
priority: priority,
95+
collapseIdentifier: collapseIdentifier,
96+
topic: topic,
97+
logger: logger ?? self.logger)
98+
}
99+
100+
/**
101+
APNSwiftConnection send method. Sends a notification to the desired deviceToken.
102+
- Parameter notification: the notification meta data and alert to send.
103+
- Parameter pushType: push type of the notification.
104+
- Parameter deviceToken: device token to send alert to.
105+
- Parameter encoder: customer JSON encoder if needed.
106+
- Parameter expiration: a date that the notificaiton expires.
107+
- Parameter priority: priority to send the notification with.
108+
- Parameter collapseIdentifier: a collapse identifier to use for grouping notifications
109+
- Parameter topic: the bundle identifier that this notification belongs to.
110+
111+
For more information see:
112+
[Retrieve Your App's Device Token](https://developer.apple.com/documentation/usernotifications/registering_your_app_with_apns#2942135)
113+
### Usage Example: ###
114+
```
115+
let apns = APNSwiftConnection.connect()
116+
let expiry = Date().addingTimeInterval(5)
117+
try apns.send(notification, pushType: .alert, to: "b27a07be2092c7fbb02ab5f62f3135c615e18acc0ddf39a30ffde34d41665276", with: JSONEncoder(), expiration: expiry, priority: 10, collapseIdentifier: "huro2").wait()
118+
```
119+
*/
120+
public func send<Notification>(_ notification: Notification,
121+
pushType: APNSwiftConnection.PushType = .alert,
122+
to deviceToken: String,
123+
with encoder: JSONEncoder = JSONEncoder(),
124+
expiration: Date? = nil,
125+
priority: Int? = nil,
126+
collapseIdentifier: String? = nil,
127+
topic: String? = nil,
128+
logger: Logger? = nil) -> EventLoopFuture<Void>
129+
where Notification: APNSwiftNotification {
130+
do {
131+
let data: Data = try encoder.encode(notification)
132+
return self.send(raw: data,
133+
pushType: pushType,
134+
to: deviceToken,
135+
expiration: expiration,
136+
priority: priority,
137+
collapseIdentifier: collapseIdentifier,
138+
topic: topic,
139+
logger: logger ?? self.logger)
140+
} catch {
141+
return self.eventLoop.makeFailedFuture(error)
142+
}
143+
}
144+
145+
/// This is to be used with caution. APNSwift cannot gurantee delivery if you do not have the correct payload.
146+
/// For more information see: [Creating APN Payload](https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html)
147+
public func send<Bytes>(raw payload: Bytes,
148+
pushType: APNSwiftConnection.PushType = .alert,
149+
to deviceToken: String,
150+
expiration: Date?,
151+
priority: Int?,
152+
collapseIdentifier: String?,
153+
topic: String?,
154+
logger: Logger? = nil) -> EventLoopFuture<Void>
155+
where Bytes : Collection, Bytes.Element == UInt8 {
156+
var buffer = ByteBufferAllocator().buffer(capacity: payload.count)
157+
buffer.writeBytes(payload)
158+
return self.send(rawBytes: buffer,
159+
pushType: pushType,
160+
to: deviceToken,
161+
expiration: expiration,
162+
priority: priority,
163+
collapseIdentifier: collapseIdentifier,
164+
topic: topic,
165+
logger: logger ?? self.logger)
166+
}
167+
168+
public func send(rawBytes payload: ByteBuffer,
169+
pushType: APNSwiftConnection.PushType = .alert,
170+
to deviceToken: String,
171+
expiration: Date? = nil,
172+
priority: Int? = nil,
173+
collapseIdentifier: String? = nil,
174+
topic: String? = nil,
175+
logger: Logger? = nil) -> EventLoopFuture<Void> {
176+
return self.send(
177+
rawBytes: payload,
178+
pushType: pushType,
179+
to: deviceToken,
180+
expiration: expiration,
181+
priority: priority,
182+
collapseIdentifier: collapseIdentifier,
183+
topic: topic,
184+
logger: logger ?? self.logger
185+
)
186+
}
187+
}
188+
189+
private struct BasicNotification: APNSwiftNotification {
190+
let aps: APNSwiftPayload
191+
}

Sources/APNSwift/APNSwiftConfiguration.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public struct APNSwiftConfiguration {
9898
)
9999
)
100100
```
101-
*/
101+
*/
102102
public init(privateKeyPath: String, pemPath: String, topic: String, environment: APNSwiftConfiguration.Environment, logger: Logger? = nil) throws {
103103
try self.init(keyIdentifier: "", teamIdentifier: "", signer: APNSwiftSigner(buffer: ByteBufferAllocator().buffer(capacity: 1024)), topic: topic, environment: environment, logger: logger)
104104
let key = try NIOSSLPrivateKey(file: privateKeyPath, format: .pem)

0 commit comments

Comments
 (0)