Skip to content

Commit

Permalink
[feat] Add support for sending apns-id (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
mackoj committed Jan 8, 2021
1 parent c6a570c commit ce8213a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
34 changes: 22 additions & 12 deletions Sources/APNSwift/APNSwiftClient.swift
Expand Up @@ -27,7 +27,8 @@ public protocol APNSwiftClient {
priority: Int?,
collapseIdentifier: String?,
topic: String?,
logger: Logger?) -> EventLoopFuture<Void>
logger: Logger?,
apnsID: UUID?) -> EventLoopFuture<Void>
}

extension APNSwiftClient {
Expand Down Expand Up @@ -59,7 +60,8 @@ extension APNSwiftClient {
priority: Int? = nil,
collapseIdentifier: String? = nil,
topic: String? = nil,
logger: Logger? = nil) -> EventLoopFuture<Void> {
logger: Logger? = nil,
apnsID: UUID? = nil) -> EventLoopFuture<Void> {
return self.send(APNSwiftPayload(alert: alert),
pushType: pushType,
to: deviceToken,
Expand All @@ -68,7 +70,8 @@ extension APNSwiftClient {
priority: priority,
collapseIdentifier: collapseIdentifier,
topic: topic,
logger: logger ?? self.logger)
logger: logger ?? self.logger,
apnsID: apnsID)
}

/**
Expand Down Expand Up @@ -99,7 +102,8 @@ extension APNSwiftClient {
priority: Int? = nil,
collapseIdentifier: String? = nil,
topic: String? = nil,
logger: Logger? = nil) -> EventLoopFuture<Void> {
logger: Logger? = nil,
apnsID: UUID? = nil) -> EventLoopFuture<Void> {
return self.send(BasicNotification(aps: payload),
pushType: pushType,
to: deviceToken,
Expand All @@ -108,7 +112,8 @@ extension APNSwiftClient {
priority: priority,
collapseIdentifier: collapseIdentifier,
topic: topic,
logger: logger ?? self.logger)
logger: logger ?? self.logger,
apnsID: apnsID)
}

/**
Expand Down Expand Up @@ -139,7 +144,8 @@ extension APNSwiftClient {
priority: Int? = nil,
collapseIdentifier: String? = nil,
topic: String? = nil,
logger: Logger? = nil) -> EventLoopFuture<Void>
logger: Logger? = nil,
apnsID: UUID? = nil) -> EventLoopFuture<Void>
where Notification: APNSwiftNotification {
do {
let data: Data = try encoder.encode(notification)
Expand All @@ -150,7 +156,8 @@ extension APNSwiftClient {
priority: priority,
collapseIdentifier: collapseIdentifier,
topic: topic,
logger: logger ?? self.logger)
logger: logger ?? self.logger,
apnsID: apnsID)
} catch {
return self.eventLoop.makeFailedFuture(error)
}
Expand All @@ -165,7 +172,8 @@ extension APNSwiftClient {
priority: Int?,
collapseIdentifier: String?,
topic: String?,
logger: Logger? = nil) -> EventLoopFuture<Void>
logger: Logger? = nil,
apnsID: UUID? = nil) -> EventLoopFuture<Void>
where Bytes : Collection, Bytes.Element == UInt8 {
var buffer = ByteBufferAllocator().buffer(capacity: payload.count)
buffer.writeBytes(payload)
Expand All @@ -176,7 +184,8 @@ extension APNSwiftClient {
priority: priority,
collapseIdentifier: collapseIdentifier,
topic: topic,
logger: logger ?? self.logger)
logger: logger ?? self.logger,
apnsID: apnsID)
}

public func send(rawBytes payload: ByteBuffer,
Expand All @@ -186,7 +195,8 @@ extension APNSwiftClient {
priority: Int? = nil,
collapseIdentifier: String? = nil,
topic: String? = nil,
logger: Logger? = nil) -> EventLoopFuture<Void> {
logger: Logger? = nil,
apnsID: UUID? = nil) -> EventLoopFuture<Void> {
return self.send(
rawBytes: payload,
pushType: pushType,
Expand All @@ -195,8 +205,8 @@ extension APNSwiftClient {
priority: priority,
collapseIdentifier: collapseIdentifier,
topic: topic,
logger: logger ?? self.logger
)
logger: logger ?? self.logger,
apnsID: apnsID)
}
}

Expand Down
6 changes: 4 additions & 2 deletions Sources/APNSwift/APNSwiftConnection.swift
Expand Up @@ -168,7 +168,8 @@ public final class APNSwiftConnection: APNSwiftClient {
priority: Int?,
collapseIdentifier: String?,
topic: String?,
logger: Logger?
logger: Logger?,
apnsID: UUID?
) -> EventLoopFuture<Void> {
let logger = logger ?? self.configuration.logger
logger?.debug("Send - starting up")
Expand All @@ -185,7 +186,8 @@ public final class APNSwiftConnection: APNSwiftClient {
priority: priority,
collapseIdentifier: collapseIdentifier,
topic: topic,
logger: logger
logger: logger,
apnsID: apnsID
),
APNSwiftResponseDecoder(),
APNSwiftStreamHandler(logger: logger)
Expand Down
14 changes: 10 additions & 4 deletions Sources/APNSwift/APNSwiftRequestEncoder.swift
Expand Up @@ -35,6 +35,7 @@ internal final class APNSwiftRequestEncoder: ChannelOutboundHandler {
let topic: String?
let pushType: APNSwiftConnection.PushType?
let logger: Logger?
let apnsID: UUID?

init(
deviceToken: String,
Expand All @@ -45,7 +46,8 @@ internal final class APNSwiftRequestEncoder: ChannelOutboundHandler {
priority: Int?,
collapseIdentifier: String?,
topic: String?,
logger: Logger?
logger: Logger?,
apnsID: UUID?
) {
self.configuration = configuration
self.bearerToken = bearerToken
Expand All @@ -56,11 +58,11 @@ internal final class APNSwiftRequestEncoder: ChannelOutboundHandler {
self.topic = topic
self.pushType = pushType
self.logger = logger
self.apnsID = apnsID
}

convenience init(deviceToken: String, configuration: APNSwiftConfiguration, bearerToken: String, expiration: Date?, priority: Int?, collapseIdentifier: String?, topic: String? = nil, logger: Logger? = nil) {
self.init(deviceToken: deviceToken, configuration: configuration, bearerToken: bearerToken, pushType: .alert, expiration: expiration, priority: priority, collapseIdentifier: collapseIdentifier, topic: topic, logger: logger)

convenience init(deviceToken: String, configuration: APNSwiftConfiguration, bearerToken: String, expiration: Date?, priority: Int?, collapseIdentifier: String?, topic: String? = nil, logger: Logger? = nil, apnsID: UUID? = nil) {
self.init(deviceToken: deviceToken, configuration: configuration, bearerToken: bearerToken, pushType: .alert, expiration: expiration, priority: priority, collapseIdentifier: collapseIdentifier, topic: topic, logger: logger, apnsID: apnsID)
}

/// See `ChannelOutboundHandler.write(context:data:promise:)`.
Expand Down Expand Up @@ -94,6 +96,10 @@ internal final class APNSwiftRequestEncoder: ChannelOutboundHandler {
if let bearerToken = self.bearerToken {
reqHead.headers.add(name: "authorization", value: "bearer \(bearerToken)")
}
if let apnsID = self.apnsID {
reqHead.headers.add(name: "apns-id", value: apnsID.uuidString.lowercased())
}

logger?.trace("Request - built")
context.write(wrapOutboundOut(.head(reqHead))).cascadeFailure(to: promise)
context.write(wrapOutboundOut(.body(.byteBuffer(buffer)))).cascadeFailure(to: promise)
Expand Down

0 comments on commit ce8213a

Please sign in to comment.