Skip to content

Commit 33717cb

Browse files
authored
Adds apns-push-type for iOS 13 (#46)
* Adds apns-push-type for iOS 13 * Allow Topic and type to be nil. * Use enum for push TYpe. * Changes initlization to be non braking. * Updates for non breaking. * Function fix. * Adds return statement. * updates. * Remove unused Generic.
1 parent 863056a commit 33717cb

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

Sources/APNSwift/APNSwiftConfiguration.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,10 @@ extension APNSwiftConfiguration {
7373
case sandbox
7474
}
7575
}
76+
77+
extension APNSwiftConnection {
78+
public enum PushType: String {
79+
case alert
80+
case background
81+
}
82+
}

Sources/APNSwift/APNSwiftConnection.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,12 @@ public final class APNSwiftConnection {
9494
try apns.send(notification, bearerToken: bearerToken,to: "b27a07be2092c7fbb02ab5f62f3135c615e18acc0ddf39a30ffde34d41665276", with: JSONEncoder(), expiration: expiry, priority: 10, collapseIdentifier: "huro2").wait()
9595
```
9696
*/
97-
public func send<Notification>(_ notification: Notification, bearerToken: APNSwiftBearerToken, to deviceToken: String, with encoder: JSONEncoder = JSONEncoder(), expiration: Date? = nil, priority: Int? = nil, collapseIdentifier: String? = nil, topic: String? = nil) -> EventLoopFuture<Void>
98-
where Notification: APNSwiftNotification {
97+
public func send<Notification: APNSwiftNotification>(_ notification: Notification, pushType: APNSwiftConnection.PushType, bearerToken: APNSwiftBearerToken, to deviceToken: String, with encoder: JSONEncoder = JSONEncoder(), expiration: Date? = nil, priority: Int? = nil, collapseIdentifier: String? = nil, topic: String? = nil) -> EventLoopFuture<Void> {
9998
let streamPromise = channel.eventLoop.makePromise(of: Channel.self)
10099
multiplexer.createStreamChannel(promise: streamPromise) { channel, streamID in
101100
let handlers: [ChannelHandler] = [
102101
HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https),
103-
APNSwiftRequestEncoder<Notification>(deviceToken: deviceToken, configuration: self.configuration, bearerToken: bearerToken, expiration: expiration, priority: priority, collapseIdentifier: collapseIdentifier),
102+
APNSwiftRequestEncoder(deviceToken: deviceToken, configuration: self.configuration, bearerToken: bearerToken, pushType: pushType, expiration: expiration, priority: priority, collapseIdentifier: collapseIdentifier, topic: topic),
104103
APNSwiftResponseDecoder(),
105104
APNSwiftStreamHandler(),
106105
]
@@ -124,6 +123,9 @@ public final class APNSwiftConnection {
124123
responsePromise.futureResult
125124
}
126125
}
126+
public func send<Notification: APNSwiftNotification>(_ notification: Notification, bearerToken: APNSwiftBearerToken, to deviceToken: String, with encoder: JSONEncoder = JSONEncoder(), expiration: Date? = nil, priority: Int? = nil, collapseIdentifier: String? = nil, topic: String? = nil) -> EventLoopFuture<Void> {
127+
return self.send(notification, pushType: .alert, bearerToken: bearerToken, to: deviceToken, with: encoder, expiration: expiration, priority: priority, collapseIdentifier: collapseIdentifier, topic: topic)
128+
}
127129

128130
var onClose: EventLoopFuture<Void> {
129131
return channel.closeFuture

Sources/APNSwift/APNSwiftRequestEncoder.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ import NIOHTTP1
1818
import NIOHTTP2
1919

2020
/// The class provides the HTTP2 interface to Swift NIO 2
21-
internal final class APNSwiftRequestEncoder<Notification>: ChannelOutboundHandler
22-
where Notification: APNSwiftNotification {
21+
internal final class APNSwiftRequestEncoder: ChannelOutboundHandler {
2322
/// See `ChannelOutboundHandler.OutboundIn`.
2423
typealias OutboundIn = ByteBuffer
2524

@@ -33,16 +32,23 @@ internal final class APNSwiftRequestEncoder<Notification>: ChannelOutboundHandle
3332
let expiration: Date?
3433
let collapseIdentifier: String?
3534
let topic: String?
35+
let pushType: APNSwiftConnection.PushType?
3636

3737

38-
init(deviceToken: String, configuration: APNSwiftConfiguration, bearerToken: APNSwiftBearerToken, expiration: Date?, priority: Int?, collapseIdentifier: String?, topic: String? = nil) {
38+
init(deviceToken: String, configuration: APNSwiftConfiguration, bearerToken: APNSwiftBearerToken, pushType: APNSwiftConnection.PushType, expiration: Date?, priority: Int?, collapseIdentifier: String?, topic: String?) {
3939
self.configuration = configuration
4040
self.bearerToken = bearerToken
4141
self.deviceToken = deviceToken
4242
self.expiration = expiration
4343
self.priority = priority
4444
self.collapseIdentifier = collapseIdentifier
4545
self.topic = topic
46+
self.pushType = pushType
47+
}
48+
49+
convenience init(deviceToken: String, configuration: APNSwiftConfiguration, bearerToken: APNSwiftBearerToken, expiration: Date?, priority: Int?, collapseIdentifier: String?, topic: String? = nil) {
50+
self.init(deviceToken: deviceToken, configuration: configuration, bearerToken: bearerToken, pushType: .alert, expiration: expiration, priority: priority, collapseIdentifier: collapseIdentifier, topic: topic)
51+
4652
}
4753

4854
/// See `ChannelOutboundHandler.write(context:data:promise:)`.
@@ -67,6 +73,9 @@ internal final class APNSwiftRequestEncoder<Notification>: ChannelOutboundHandle
6773
if let collapseId = self.collapseIdentifier {
6874
reqHead.headers.add(name: "apns-collapse-id", value: collapseId)
6975
}
76+
if let pushType = self.pushType {
77+
reqHead.headers.add(name: "apns-push-type", value: pushType.rawValue)
78+
}
7079
reqHead.headers.add(name: "host", value: configuration.url.host!)
7180
guard let token = bearerToken.token else {
7281
promise?.fail(APNSwiftError.SigningError.invalidSignatureData)

Tests/APNSwiftTests/APNSwiftRequestTests.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ import XCTest
2323

2424
final class APNSwiftRequestTests: XCTestCase {
2525

26-
struct BasicNotification: APNSwiftNotification {
27-
let aps: APNSwiftPayload
28-
29-
init(aps: APNSwiftPayload) {
30-
self.aps = aps
31-
}
32-
}
3326
func testAlertEncoding() throws {
3427
let alert = APNSwiftPayload.APNSwiftAlert(title: "title", subtitle: "subtitle", body: "body", titleLocKey: "titlelockey",
3528
titleLocArgs: ["titlelocarg1"], actionLocKey: "actionkey", locKey: "lockey", locArgs: ["locarg1"], launchImage: "launchImage")
@@ -178,6 +171,13 @@ final class APNSwiftRequestTests: XCTestCase {
178171
}
179172

180173
func testInvalidAuthKey() throws {
174+
struct BasicNotification: APNSwiftNotification {
175+
let aps: APNSwiftPayload
176+
177+
init(aps: APNSwiftPayload) {
178+
self.aps = aps
179+
}
180+
}
181181
let deviceToken = ""
182182
let allocator = ByteBufferAllocator()
183183
var signerBuffer = allocator.buffer(capacity: invalidAuthKey.count)
@@ -190,7 +190,8 @@ final class APNSwiftRequestTests: XCTestCase {
190190
topic: "com.grasscove.Fern",
191191
environment: .sandbox)
192192
let token = APNSwiftBearerToken(configuration: apnsConfig, timeout: 50.0)
193-
let channel = EmbeddedChannel(handler: APNSwiftRequestEncoder<BasicNotification>(deviceToken: deviceToken, configuration: apnsConfig, bearerToken: token, expiration: nil, priority: nil, collapseIdentifier: nil))
193+
let handler: APNSwiftRequestEncoder = .init(deviceToken: deviceToken, configuration: apnsConfig, bearerToken: token, pushType: .alert, expiration: nil, priority: nil, collapseIdentifier: nil, topic: nil)
194+
let channel = EmbeddedChannel(handler: handler)
194195

195196
// pretend to connect the connect (nothing real will happen)
196197
XCTAssertNoThrow(try channel.connect(to: .init(ipAddress: "1.2.3.4", port: 5)).wait())

0 commit comments

Comments
 (0)