@@ -85,7 +85,6 @@ public final class APNSwiftConnection {
8585
8686 let sslContext = try ! NIOSSLContext ( configuration: configuration. tlsConfiguration)
8787 let connectionFullyUpPromise = eventLoop. makePromise ( of: Void . self)
88-
8988 let tcpConnection = ClientBootstrap ( group: eventLoop) . connect ( host: configuration. url. host!, port: 443 )
9089 tcpConnection. cascadeFailure ( to: connectionFullyUpPromise)
9190 return tcpConnection. flatMap { channel in
@@ -96,8 +95,16 @@ public final class APNSwiftConnection {
9695 channel. configureHTTP2Pipeline ( mode: . client) { channel, _ in
9796 return channel. eventLoop. makeFailedFuture ( UnsupportedServerPushError ( ) )
9897 } . flatMap { multiplexer in
99- connectionFullyUpPromise. futureResult. map {
100- return APNSwiftConnection ( channel: channel, multiplexer: multiplexer, configuration: configuration)
98+ var tokenFactory : APNSwiftBearerTokenFactory ? = nil
99+ if configuration. tlsConfiguration. privateKey == nil {
100+ do {
101+ tokenFactory = try APNSwiftBearerTokenFactory ( eventLoop: eventLoop, configuration: configuration)
102+ } catch {
103+ return channel. eventLoop. makeFailedFuture ( APNSwiftError . SigningError. invalidSignatureData)
104+ }
105+ }
106+ return connectionFullyUpPromise. futureResult. map { ( ) -> APNSwiftConnection in
107+ return APNSwiftConnection ( channel: channel, multiplexer: multiplexer, configuration: configuration, bearerTokenFactory: tokenFactory)
101108 }
102109 }
103110 }
@@ -107,11 +114,22 @@ public final class APNSwiftConnection {
107114 public let multiplexer : HTTP2StreamMultiplexer
108115 public let channel : Channel
109116 public let configuration : APNSwiftConfiguration
117+ private var bearerTokenFactory : APNSwiftBearerTokenFactory ?
110118
111- public init ( channel: Channel , multiplexer: HTTP2StreamMultiplexer , configuration: APNSwiftConfiguration ) {
119+ private init ( channel: Channel , multiplexer: HTTP2StreamMultiplexer , configuration: APNSwiftConfiguration , bearerTokenFactory : APNSwiftBearerTokenFactory ? ) {
112120 self . channel = channel
113121 self . multiplexer = multiplexer
114122 self . configuration = configuration
123+ self . bearerTokenFactory = bearerTokenFactory
124+ }
125+
126+ @available ( * , deprecated, message: " APNSwiftConnection is initialized internally now. " )
127+ public convenience init ( channel: Channel , multiplexer: HTTP2StreamMultiplexer , configuration: APNSwiftConfiguration ) {
128+ var tokenFactory : APNSwiftBearerTokenFactory ? = nil
129+ if configuration. tlsConfiguration. privateKey == nil {
130+ tokenFactory = try ? APNSwiftBearerTokenFactory ( eventLoop: channel. eventLoop, configuration: configuration)
131+ }
132+ self . init ( channel: channel, multiplexer: multiplexer, configuration: configuration, bearerTokenFactory: tokenFactory)
115133 }
116134
117135 /**
@@ -135,12 +153,12 @@ public final class APNSwiftConnection {
135153 try apns.send(notification, bearerToken: bearerToken,to: "b27a07be2092c7fbb02ab5f62f3135c615e18acc0ddf39a30ffde34d41665276", with: JSONEncoder(), expiration: expiry, priority: 10, collapseIdentifier: "huro2").wait()
136154 ```
137155 */
138- 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 > {
156+ public func send< Notification: APNSwiftNotification > ( _ notification: Notification , pushType: APNSwiftConnection . PushType , to deviceToken: String , with encoder: JSONEncoder = JSONEncoder ( ) , expiration: Date ? = nil , priority: Int ? = nil , collapseIdentifier: String ? = nil , topic: String ? = nil ) -> EventLoopFuture < Void > {
139157 let streamPromise = channel. eventLoop. makePromise ( of: Channel . self)
140158 multiplexer. createStreamChannel ( promise: streamPromise) { channel, streamID in
141159 let handlers : [ ChannelHandler ] = [
142160 HTTP2ToHTTP1ClientCodec ( streamID: streamID, httpProtocol: . https) ,
143- APNSwiftRequestEncoder ( deviceToken: deviceToken, configuration: self . configuration, bearerToken: bearerToken , pushType: pushType, expiration: expiration, priority: priority, collapseIdentifier: collapseIdentifier, topic: topic) ,
161+ APNSwiftRequestEncoder ( deviceToken: deviceToken, configuration: self . configuration, bearerToken: self . bearerTokenFactory ? . currentBearerToken , pushType: pushType, expiration: expiration, priority: priority, collapseIdentifier: collapseIdentifier, topic: topic) ,
144162 APNSwiftResponseDecoder ( ) ,
145163 APNSwiftStreamHandler ( ) ,
146164 ]
@@ -163,15 +181,20 @@ public final class APNSwiftConnection {
163181 responsePromise. futureResult
164182 }
165183 }
184+ @available ( * , deprecated, message: " Bearer Tokens are handled internally now, and no longer exposed. " )
166185 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 > {
167- return self . send ( notification, pushType: . alert, bearerToken : bearerToken , to: deviceToken, with: encoder, expiration: expiration, priority: priority, collapseIdentifier: collapseIdentifier, topic: topic)
186+ return self . send ( notification, pushType: . alert, to: deviceToken, with: encoder, expiration: expiration, priority: priority, collapseIdentifier: collapseIdentifier, topic: topic)
168187 }
169188
170189 var onClose : EventLoopFuture < Void > {
171190 return channel. closeFuture
172191 }
173192
174193 public func close( ) -> EventLoopFuture < Void > {
194+ channel. eventLoop. execute {
195+ self . bearerTokenFactory? . cancel ( )
196+ self . bearerTokenFactory = nil
197+ }
175198 return channel. close ( mode: . all)
176199 }
177200}
0 commit comments