@@ -5,7 +5,7 @@ public struct RegisterAPNSID {
5
5
let appBundleId : String
6
6
let serverKey : String ?
7
7
let sandbox : Bool
8
-
8
+
9
9
public init ( appBundleId: String , serverKey: String ? = nil , sandbox: Bool = false ) {
10
10
self . appBundleId = appBundleId
11
11
self . serverKey = serverKey
@@ -37,7 +37,7 @@ public struct APNSToFirebaseToken {
37
37
extension FCM {
38
38
/// Helper method which registers your pure APNS token in Firebase Cloud Messaging
39
39
/// and returns firebase tokens for each APNS token
40
- ///
40
+ ///
41
41
/// Convenient way
42
42
///
43
43
/// Declare `RegisterAPNSID` via extension
@@ -53,7 +53,7 @@ extension FCM {
53
53
on eventLoop: EventLoop ? = nil ) -> EventLoopFuture < [ APNSToFirebaseToken ] > {
54
54
registerAPNS ( appBundleId: id. appBundleId, serverKey: id. serverKey, sandbox: id. sandbox, tokens: tokens, on: eventLoop)
55
55
}
56
-
56
+
57
57
/// Helper method which registers your pure APNS token in Firebase Cloud Messaging
58
58
/// and returns firebase tokens for each APNS token
59
59
///
@@ -72,7 +72,7 @@ extension FCM {
72
72
on eventLoop: EventLoop ? = nil ) -> EventLoopFuture < [ APNSToFirebaseToken ] > {
73
73
registerAPNS ( appBundleId: id. appBundleId, serverKey: id. serverKey, sandbox: id. sandbox, tokens: tokens, on: eventLoop)
74
74
}
75
-
75
+
76
76
/// Helper method which registers your pure APNS token in Firebase Cloud Messaging
77
77
/// and returns firebase tokens for each APNS token
78
78
public func registerAPNS(
@@ -83,7 +83,7 @@ extension FCM {
83
83
on eventLoop: EventLoop ? = nil ) -> EventLoopFuture < [ APNSToFirebaseToken ] > {
84
84
registerAPNS ( appBundleId: appBundleId, serverKey: serverKey, sandbox: sandbox, tokens: tokens, on: eventLoop)
85
85
}
86
-
86
+
87
87
/// Helper method which registers your pure APNS token in Firebase Cloud Messaging
88
88
/// and returns firebase tokens for each APNS token
89
89
public func registerAPNS(
@@ -110,43 +110,30 @@ extension FCM {
110
110
fatalError ( " FCM: Register APNS: Server Key is missing. " )
111
111
}
112
112
let url = iidURL + " batchImport "
113
- return eventLoop. future ( ) . flatMapThrowing { accessToken throws -> HTTPClient . Request in
114
- struct Payload : Codable {
113
+
114
+ var headers = HTTPHeaders ( )
115
+ headers. add ( name: . authorization, value: " key= \( serverKey) " )
116
+
117
+ return self . client. post ( URI ( string: url) , headers: headers) { ( req) in
118
+ struct Payload : Content {
115
119
let application : String
116
120
let sandbox : Bool
117
121
let apns_tokens : [ String ]
118
122
}
119
123
let payload = Payload ( application: appBundleId, sandbox: sandbox, apns_tokens: tokens)
120
- let payloadData = try JSONEncoder ( ) . encode ( payload)
121
-
122
- var headers = HTTPHeaders ( )
123
- headers. add ( name: " Authorization " , value: " key= \( serverKey) " )
124
- headers. add ( name: " Content-Type " , value: " application/json " )
125
-
126
- return try . init( url: url, method: . POST, headers: headers, body: . data( payloadData) )
127
- } . flatMap { request in
128
- return self . client. execute ( request: request) . flatMapThrowing { res in
129
- guard 200 ..< 300 ~= res. status. code else {
130
- guard
131
- let bb = res. body,
132
- let bytes = bb. getBytes ( at: 0 , length: bb. readableBytes) ,
133
- let reason = String ( bytes: bytes, encoding: . utf8) else {
134
- throw Abort ( . internalServerError, reason: " FCM: Register APNS: unable to decode error response " )
135
- }
136
- throw Abort ( . internalServerError, reason: reason)
137
- }
124
+ try req. content. encode ( payload)
125
+ }
126
+ . validate ( )
127
+ . flatMapThrowing { res in
128
+ struct Result : Codable {
138
129
struct Result : Codable {
139
- struct Result : Codable {
140
- let registration_token , apns_token , status : String
141
- }
142
- var results : [ Result ]
143
- }
144
- guard let body = res. body, let result = try ? JSONDecoder ( ) . decode ( Result . self, from: body) else {
145
- throw Abort ( . notFound, reason: " FCM: Register APNS: empty response " )
146
- }
147
- return result. results. map {
148
- . init( registration_token: $0. registration_token, apns_token: $0. apns_token, isRegistered: $0. status == " OK " )
130
+ let registration_token , apns_token , status : String
149
131
}
132
+ let results : [ Result ]
133
+ }
134
+ let result = try res. content. decode ( Result . self)
135
+ return result. results. map {
136
+ . init( registration_token: $0. registration_token, apns_token: $0. apns_token, isRegistered: $0. status == " OK " )
150
137
}
151
138
}
152
139
}
0 commit comments