@@ -18,6 +18,26 @@ import NIOHTTP2
1818import NIOSSL
1919
2020public final class APNSwiftConnection {
21+
22+ /**
23+ APNSwift Connect method. Used to establish a connection with Apple Push Notification service.
24+ - Parameter configuration: APNSwiftConfiguration struct.
25+ - Parameter eventLoop: eventLoop to open the connection on.
26+
27+ ### Usage Example: ###
28+ ```
29+ let signer = try! APNSwiftSigner(filePath: "/Users/kylebrowning/Downloads/AuthKey_9UC9ZLQ8YW.p8")
30+
31+ let apnsConfig = APNSwiftConfiguration(keyIdentifier: "9UC9ZLQ8YW",
32+ teamIdentifier: "ABBM6U9RM5",
33+ signer: signer,
34+ topic: "com.grasscove.Fern",
35+ environment: .sandbox)
36+
37+ let apns = try APNSwiftConnection.connect(configuration: apnsConfig, on: group.next()).wait()
38+ ```
39+ */
40+
2141 public static func connect( configuration: APNSwiftConfiguration , on eventLoop: EventLoop ) -> EventLoopFuture < APNSwiftConnection > {
2242 let bootstrap = ClientBootstrap ( group: eventLoop)
2343 . channelOption ( ChannelOptions . socket ( IPPROTO_TCP, TCP_NODELAY) , value: 1 )
@@ -34,65 +54,79 @@ public final class APNSwiftConnection {
3454 channel. close ( mode: . all, promise: nil )
3555 return channel. eventLoop. makeFailedFuture ( error)
3656 }
37- }
38-
57+ }
58+
3959 return bootstrap. connect ( host: configuration. url. host!, port: 443 ) . flatMap { channel in
4060 return channel. pipeline. handler ( type: HTTP2StreamMultiplexer . self) . map { multiplexer in
4161 return APNSwiftConnection ( channel: channel, multiplexer: multiplexer, configuration: configuration)
4262 }
4363 }
4464 }
45-
65+
4666 public let multiplexer : HTTP2StreamMultiplexer
4767 public let channel : Channel
4868 public let configuration : APNSwiftConfiguration
49-
69+
5070 public init ( channel: Channel , multiplexer: HTTP2StreamMultiplexer , configuration: APNSwiftConfiguration ) {
5171 self . channel = channel
5272 self . multiplexer = multiplexer
5373 self . configuration = configuration
5474 }
55-
56- public func send< Notification> ( _ notification: Notification , to deviceToken: String , with customEncoder: JSONEncoder ? = nil , expiration: Date ? = nil , priority: Int ? = nil , collapseIdentifier: String ? = nil , topic: String ? = nil ) -> EventLoopFuture < Void >
75+
76+ /**
77+ APNSwiftConnection send method. Sends a notification to the desired deviceToken.
78+ - Parameter notification: the notification meta data and alert to send.
79+ - Parameter deviceToken: device token to send alert to.
80+ - Parameter encoder: customer JSON encoder if needed.
81+ - Parameter expiration: a date that the notificaiton expires.
82+ - Parameter priority: priority to send the notification with.
83+ - Parameter collapseIdentifier: a collapse identifier to use for grouping notifications
84+ - Parameter topic: the bundle identifier that this notification belongs to.
85+
86+ For more information see:
87+ [Retrieve Your App's Device Token](https://developer.apple.com/documentation/usernotifications/registering_your_app_with_apns#2942135)
88+ ### Usage Example: ###
89+ ```
90+ let apns = APNSwiftConnection.connect()
91+ let expiry = Date().addingTimeInterval(5)
92+ try apns.send(notification, to: "b27a07be2092c7fbb02ab5f62f3135c615e18acc0ddf39a30ffde34d41665276", with: JSONEncoder(), expiration: expiry, priority: 10, collapseIdentifier: "huro2").wait()
93+ ```
94+ */
95+ public func send< Notification> ( _ notification: Notification , to deviceToken: String , with encoder: JSONEncoder = JSONEncoder ( ) , expiration: Date ? = nil , priority: Int ? = nil , collapseIdentifier: String ? = nil , topic: String ? = nil ) -> EventLoopFuture < Void >
5796 where Notification: APNSwiftNotification {
58- let streamPromise = channel. eventLoop. makePromise ( of: Channel . self)
59- multiplexer. createStreamChannel ( promise: streamPromise) { channel, streamID in
60- let handlers : [ ChannelHandler ] = [
61- HTTP2ToHTTP1ClientCodec ( streamID: streamID, httpProtocol: . https) ,
62- APNSwiftRequestEncoder < Notification > ( deviceToken: deviceToken, configuration: self . configuration, expiration: expiration, priority: priority, collapseIdentifier: collapseIdentifier) ,
63- APNSwiftResponseDecoder ( ) ,
64- APNSwiftStreamHandler ( ) ,
65- ]
66- return channel. pipeline. addHandlers ( handlers)
67- }
68-
69- let responsePromise = channel. eventLoop. makePromise ( of: Void . self)
70- let data : Data
71- if let customEncoder = customEncoder {
72- data = try ! customEncoder. encode ( notification)
73- } else {
74- data = try ! JSONEncoder ( ) . encode ( notification)
75- }
76-
77- var buffer = ByteBufferAllocator ( ) . buffer ( capacity: data. count)
78- buffer. writeBytes ( data)
79- let context = APNSwiftRequestContext (
80- request: buffer,
81- responsePromise: responsePromise
82- )
83-
84- return streamPromise. futureResult. flatMap { stream in
85- responsePromise. futureResult. whenComplete { _ in }
86- return stream. writeAndFlush ( context)
97+ let streamPromise = channel. eventLoop. makePromise ( of: Channel . self)
98+ multiplexer. createStreamChannel ( promise: streamPromise) { channel, streamID in
99+ let handlers : [ ChannelHandler ] = [
100+ HTTP2ToHTTP1ClientCodec ( streamID: streamID, httpProtocol: . https) ,
101+ APNSwiftRequestEncoder < Notification > ( deviceToken: deviceToken, configuration: self . configuration, expiration: expiration, priority: priority, collapseIdentifier: collapseIdentifier) ,
102+ APNSwiftResponseDecoder ( ) ,
103+ APNSwiftStreamHandler ( ) ,
104+ ]
105+ return channel. pipeline. addHandlers ( handlers)
106+ }
107+
108+ let responsePromise = channel. eventLoop. makePromise ( of: Void . self)
109+ let data : Data = try ! encoder. encode ( notification)
110+
111+ var buffer = ByteBufferAllocator ( ) . buffer ( capacity: data. count)
112+ buffer. writeBytes ( data)
113+ let context = APNSwiftRequestContext (
114+ request: buffer,
115+ responsePromise: responsePromise
116+ )
117+
118+ return streamPromise. futureResult. flatMap { stream in
119+ responsePromise. futureResult. whenComplete { _ in }
120+ return stream. writeAndFlush ( context)
87121 } . flatMap {
88122 responsePromise. futureResult
89- }
123+ }
90124 }
91-
125+
92126 var onClose : EventLoopFuture < Void > {
93127 return channel. closeFuture
94128 }
95-
129+
96130 public func close( ) -> EventLoopFuture < Void > {
97131 return channel. close ( mode: . all)
98132 }
0 commit comments