Skip to content

Commit 2577800

Browse files
committed
Add feature to retrieve subscribed topics
1 parent dc39421 commit 2577800

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import Foundation
2+
import Vapor
3+
4+
extension FCM {
5+
public func getTopics(token: String, on eventLoop: EventLoop) -> EventLoopFuture<[String]> {
6+
return _getTopics(token: token, on: eventLoop)
7+
}
8+
9+
public func _getTopics(token: String, on eventLoop: EventLoop) -> EventLoopFuture<[String]> {
10+
guard let configuration = self.configuration else {
11+
fatalError("FCM not configured. Use app.fcm.configuration = ...")
12+
}
13+
guard let serverKey = configuration.serverKey else {
14+
fatalError("FCM: GetTopics: Server Key is missing.")
15+
}
16+
let url = self.iidURL + "info/\(token)?details=true"
17+
return getAccessToken().flatMap { accessToken -> EventLoopFuture<ClientResponse> in
18+
var headers = HTTPHeaders()
19+
headers.add(name: .authorization, value: "key=\(serverKey)")
20+
21+
return self.client.get(URI(string: url), headers: headers)
22+
}
23+
.validate()
24+
.flatMapThrowing { response in
25+
struct Result: Codable {
26+
let rel: Relations
27+
28+
struct Relations: Codable {
29+
let topics: [String: TopicMetadata]
30+
}
31+
32+
struct TopicMetadata: Codable {
33+
let addDate: String
34+
}
35+
}
36+
let result = try response.content.decode(Result.self, using: JSONDecoder())
37+
return Array(result.rel.topics.keys)
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)