Skip to content

Commit ce8213a

Browse files
authored
[feat] Add support for sending apns-id (#99)
1 parent c6a570c commit ce8213a

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

Sources/APNSwift/APNSwiftClient.swift

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public protocol APNSwiftClient {
2727
priority: Int?,
2828
collapseIdentifier: String?,
2929
topic: String?,
30-
logger: Logger?) -> EventLoopFuture<Void>
30+
logger: Logger?,
31+
apnsID: UUID?) -> EventLoopFuture<Void>
3132
}
3233

3334
extension APNSwiftClient {
@@ -59,7 +60,8 @@ extension APNSwiftClient {
5960
priority: Int? = nil,
6061
collapseIdentifier: String? = nil,
6162
topic: String? = nil,
62-
logger: Logger? = nil) -> EventLoopFuture<Void> {
63+
logger: Logger? = nil,
64+
apnsID: UUID? = nil) -> EventLoopFuture<Void> {
6365
return self.send(APNSwiftPayload(alert: alert),
6466
pushType: pushType,
6567
to: deviceToken,
@@ -68,7 +70,8 @@ extension APNSwiftClient {
6870
priority: priority,
6971
collapseIdentifier: collapseIdentifier,
7072
topic: topic,
71-
logger: logger ?? self.logger)
73+
logger: logger ?? self.logger,
74+
apnsID: apnsID)
7275
}
7376

7477
/**
@@ -99,7 +102,8 @@ extension APNSwiftClient {
99102
priority: Int? = nil,
100103
collapseIdentifier: String? = nil,
101104
topic: String? = nil,
102-
logger: Logger? = nil) -> EventLoopFuture<Void> {
105+
logger: Logger? = nil,
106+
apnsID: UUID? = nil) -> EventLoopFuture<Void> {
103107
return self.send(BasicNotification(aps: payload),
104108
pushType: pushType,
105109
to: deviceToken,
@@ -108,7 +112,8 @@ extension APNSwiftClient {
108112
priority: priority,
109113
collapseIdentifier: collapseIdentifier,
110114
topic: topic,
111-
logger: logger ?? self.logger)
115+
logger: logger ?? self.logger,
116+
apnsID: apnsID)
112117
}
113118

114119
/**
@@ -139,7 +144,8 @@ extension APNSwiftClient {
139144
priority: Int? = nil,
140145
collapseIdentifier: String? = nil,
141146
topic: String? = nil,
142-
logger: Logger? = nil) -> EventLoopFuture<Void>
147+
logger: Logger? = nil,
148+
apnsID: UUID? = nil) -> EventLoopFuture<Void>
143149
where Notification: APNSwiftNotification {
144150
do {
145151
let data: Data = try encoder.encode(notification)
@@ -150,7 +156,8 @@ extension APNSwiftClient {
150156
priority: priority,
151157
collapseIdentifier: collapseIdentifier,
152158
topic: topic,
153-
logger: logger ?? self.logger)
159+
logger: logger ?? self.logger,
160+
apnsID: apnsID)
154161
} catch {
155162
return self.eventLoop.makeFailedFuture(error)
156163
}
@@ -165,7 +172,8 @@ extension APNSwiftClient {
165172
priority: Int?,
166173
collapseIdentifier: String?,
167174
topic: String?,
168-
logger: Logger? = nil) -> EventLoopFuture<Void>
175+
logger: Logger? = nil,
176+
apnsID: UUID? = nil) -> EventLoopFuture<Void>
169177
where Bytes : Collection, Bytes.Element == UInt8 {
170178
var buffer = ByteBufferAllocator().buffer(capacity: payload.count)
171179
buffer.writeBytes(payload)
@@ -176,7 +184,8 @@ extension APNSwiftClient {
176184
priority: priority,
177185
collapseIdentifier: collapseIdentifier,
178186
topic: topic,
179-
logger: logger ?? self.logger)
187+
logger: logger ?? self.logger,
188+
apnsID: apnsID)
180189
}
181190

182191
public func send(rawBytes payload: ByteBuffer,
@@ -186,7 +195,8 @@ extension APNSwiftClient {
186195
priority: Int? = nil,
187196
collapseIdentifier: String? = nil,
188197
topic: String? = nil,
189-
logger: Logger? = nil) -> EventLoopFuture<Void> {
198+
logger: Logger? = nil,
199+
apnsID: UUID? = nil) -> EventLoopFuture<Void> {
190200
return self.send(
191201
rawBytes: payload,
192202
pushType: pushType,
@@ -195,8 +205,8 @@ extension APNSwiftClient {
195205
priority: priority,
196206
collapseIdentifier: collapseIdentifier,
197207
topic: topic,
198-
logger: logger ?? self.logger
199-
)
208+
logger: logger ?? self.logger,
209+
apnsID: apnsID)
200210
}
201211
}
202212

Sources/APNSwift/APNSwiftConnection.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ public final class APNSwiftConnection: APNSwiftClient {
168168
priority: Int?,
169169
collapseIdentifier: String?,
170170
topic: String?,
171-
logger: Logger?
171+
logger: Logger?,
172+
apnsID: UUID?
172173
) -> EventLoopFuture<Void> {
173174
let logger = logger ?? self.configuration.logger
174175
logger?.debug("Send - starting up")
@@ -185,7 +186,8 @@ public final class APNSwiftConnection: APNSwiftClient {
185186
priority: priority,
186187
collapseIdentifier: collapseIdentifier,
187188
topic: topic,
188-
logger: logger
189+
logger: logger,
190+
apnsID: apnsID
189191
),
190192
APNSwiftResponseDecoder(),
191193
APNSwiftStreamHandler(logger: logger)

Sources/APNSwift/APNSwiftRequestEncoder.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ internal final class APNSwiftRequestEncoder: ChannelOutboundHandler {
3535
let topic: String?
3636
let pushType: APNSwiftConnection.PushType?
3737
let logger: Logger?
38+
let apnsID: UUID?
3839

3940
init(
4041
deviceToken: String,
@@ -45,7 +46,8 @@ internal final class APNSwiftRequestEncoder: ChannelOutboundHandler {
4546
priority: Int?,
4647
collapseIdentifier: String?,
4748
topic: String?,
48-
logger: Logger?
49+
logger: Logger?,
50+
apnsID: UUID?
4951
) {
5052
self.configuration = configuration
5153
self.bearerToken = bearerToken
@@ -56,11 +58,11 @@ internal final class APNSwiftRequestEncoder: ChannelOutboundHandler {
5658
self.topic = topic
5759
self.pushType = pushType
5860
self.logger = logger
61+
self.apnsID = apnsID
5962
}
6063

61-
convenience init(deviceToken: String, configuration: APNSwiftConfiguration, bearerToken: String, expiration: Date?, priority: Int?, collapseIdentifier: String?, topic: String? = nil, logger: Logger? = nil) {
62-
self.init(deviceToken: deviceToken, configuration: configuration, bearerToken: bearerToken, pushType: .alert, expiration: expiration, priority: priority, collapseIdentifier: collapseIdentifier, topic: topic, logger: logger)
63-
64+
convenience init(deviceToken: String, configuration: APNSwiftConfiguration, bearerToken: String, expiration: Date?, priority: Int?, collapseIdentifier: String?, topic: String? = nil, logger: Logger? = nil, apnsID: UUID? = nil) {
65+
self.init(deviceToken: deviceToken, configuration: configuration, bearerToken: bearerToken, pushType: .alert, expiration: expiration, priority: priority, collapseIdentifier: collapseIdentifier, topic: topic, logger: logger, apnsID: apnsID)
6466
}
6567

6668
/// See `ChannelOutboundHandler.write(context:data:promise:)`.
@@ -94,6 +96,10 @@ internal final class APNSwiftRequestEncoder: ChannelOutboundHandler {
9496
if let bearerToken = self.bearerToken {
9597
reqHead.headers.add(name: "authorization", value: "bearer \(bearerToken)")
9698
}
99+
if let apnsID = self.apnsID {
100+
reqHead.headers.add(name: "apns-id", value: apnsID.uuidString.lowercased())
101+
}
102+
97103
logger?.trace("Request - built")
98104
context.write(wrapOutboundOut(.head(reqHead))).cascadeFailure(to: promise)
99105
context.write(wrapOutboundOut(.body(.byteBuffer(buffer)))).cascadeFailure(to: promise)

0 commit comments

Comments
 (0)