-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
96738d0
commit d1bb4a5
Showing
4 changed files
with
196 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import Foundation | ||
import Vapor | ||
|
||
extension FCM { | ||
public func batchSend(_ message: FCMMessageDefault, tokens: String...) -> EventLoopFuture<[String]> { | ||
_send(message, tokens: tokens) | ||
} | ||
|
||
public func batchSend(_ message: FCMMessageDefault, tokens: String..., on eventLoop: EventLoop) -> EventLoopFuture<[String]> { | ||
_send(message, tokens: tokens).hop(to: eventLoop) | ||
} | ||
|
||
public func batchSend(_ message: FCMMessageDefault, tokens: [String]) -> EventLoopFuture<[String]> { | ||
_send(message, tokens: tokens) | ||
} | ||
|
||
public func batchSend(_ message: FCMMessageDefault, tokens: [String], on eventLoop: EventLoop) -> EventLoopFuture<[String]> { | ||
_send(message, tokens: tokens).hop(to: eventLoop) | ||
} | ||
|
||
private func _send(_ message: FCMMessageDefault, tokens: [String]) -> EventLoopFuture<[String]> { | ||
if message.apns == nil, | ||
let apnsDefaultConfig = apnsDefaultConfig { | ||
message.apns = apnsDefaultConfig | ||
} | ||
if message.android == nil, | ||
let androidDefaultConfig = androidDefaultConfig { | ||
message.android = androidDefaultConfig | ||
} | ||
if message.webpush == nil, | ||
let webpushDefaultConfig = webpushDefaultConfig { | ||
message.webpush = webpushDefaultConfig | ||
} | ||
var preparedTokens: [[String]] = [] | ||
tokens.enumerated().forEach { i, token in | ||
if Double(i).truncatingRemainder(dividingBy: 1) == 0 { | ||
preparedTokens.append([token]) | ||
} else { | ||
if var arr = preparedTokens.popLast() { | ||
arr.append(token) | ||
preparedTokens.append(arr) | ||
} else { | ||
preparedTokens.append([token]) | ||
} | ||
} | ||
} | ||
var deviceGroups: [String: [String]] = [:] | ||
return preparedTokens.map { tokens in | ||
createTopic(tokens: tokens).map { | ||
deviceGroups[$0] = tokens | ||
} | ||
}.flatten(on: application.eventLoopGroup.next()).flatMap { | ||
var results: [String] = [] | ||
return deviceGroups.map { deviceGroup in | ||
let message = message | ||
message.token = nil | ||
message.condition = nil | ||
message.topic = deviceGroup.key | ||
return self.send(message).map { | ||
results.append($0) | ||
}.flatMap { | ||
self.deleteTopic(deviceGroup.key, tokens: deviceGroup.value) | ||
} | ||
}.flatten(on: self.application.eventLoopGroup.next()).transform(to: results) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import Foundation | ||
import Vapor | ||
|
||
extension FCM { | ||
public func createTopic(_ name: String? = nil, tokens: String...) -> EventLoopFuture<String> { | ||
createTopic(name, tokens: tokens) | ||
} | ||
|
||
public func createTopic(_ name: String? = nil, tokens: String..., on eventLoop: EventLoop) -> EventLoopFuture<String> { | ||
createTopic(name, tokens: tokens).hop(to: eventLoop) | ||
} | ||
|
||
public func createTopic(_ name: String? = nil, tokens: [String]) -> EventLoopFuture<String> { | ||
_createTopic(name, tokens: tokens) | ||
} | ||
|
||
public func createTopic(_ name: String? = nil, tokens: [String], on eventLoop: EventLoop) -> EventLoopFuture<String> { | ||
_createTopic(name, tokens: tokens).hop(to: eventLoop) | ||
} | ||
|
||
private func _createTopic(_ name: String? = nil, tokens: [String]) -> EventLoopFuture<String> { | ||
guard let configuration = self.configuration else { | ||
fatalError("FCM not configured. Use app.fcm.configuration = ...") | ||
} | ||
guard let serverKey = configuration.serverKey else { | ||
fatalError("FCM: CreateTopic: Server Key is missing.") | ||
} | ||
let url = self.iidURL + "batchAdd" | ||
let name = name ?? UUID().uuidString | ||
return getAccessToken().flatMapThrowing { accessToken throws -> HTTPClient.Request in | ||
struct Payload: Codable { | ||
let to: String | ||
let registration_tokens: [String] | ||
|
||
init (to: String, registration_tokens: [String]) { | ||
self.to = "/topics/\(to)" | ||
self.registration_tokens = registration_tokens | ||
} | ||
} | ||
let payload = Payload(to: name, registration_tokens: tokens) | ||
let payloadData = try JSONEncoder().encode(payload) | ||
var headers = HTTPHeaders() | ||
headers.add(name: "Authorization", value: "key=\(serverKey)") | ||
headers.add(name: "Content-Type", value: "application/json") | ||
return try .init(url: url, method: .POST, headers: headers, body: .data(payloadData)) | ||
}.flatMap { request in | ||
return self.client.execute(request: request).flatMapThrowing { res in | ||
guard 200 ..< 300 ~= res.status.code else { | ||
if let body = res.body, let googleError = try? JSONDecoder().decode(GoogleError.self, from: body) { | ||
throw googleError | ||
} else { | ||
guard | ||
let bb = res.body, | ||
let bytes = bb.getBytes(at: 0, length: bb.readableBytes), | ||
let reason = String(bytes: bytes, encoding: .utf8) else { | ||
throw Abort(.internalServerError, reason: "FCM: CreateTopic: unable to decode error response") | ||
} | ||
throw Abort(.internalServerError, reason: reason) | ||
} | ||
} | ||
return name | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import Foundation | ||
import Vapor | ||
|
||
extension FCM { | ||
public func deleteTopic(_ name: String, tokens: String...) -> EventLoopFuture<Void> { | ||
deleteTopic(name, tokens: tokens) | ||
} | ||
|
||
public func deleteTopic(_ name: String, tokens: String..., on eventLoop: EventLoop) -> EventLoopFuture<Void> { | ||
deleteTopic(name, tokens: tokens).hop(to: eventLoop) | ||
} | ||
|
||
public func deleteTopic(_ name: String, tokens: [String]) -> EventLoopFuture<Void> { | ||
_deleteTopic(name, tokens: tokens) | ||
} | ||
|
||
public func deleteTopic(_ name: String, tokens: [String], on eventLoop: EventLoop) -> EventLoopFuture<Void> { | ||
_deleteTopic(name, tokens: tokens).hop(to: eventLoop) | ||
} | ||
|
||
private func _deleteTopic(_ name: String, tokens: [String]) -> EventLoopFuture<Void> { | ||
guard let configuration = self.configuration else { | ||
fatalError("FCM not configured. Use app.fcm.configuration = ...") | ||
} | ||
guard let serverKey = configuration.serverKey else { | ||
fatalError("FCM: DeleteTopic: Server Key is missing.") | ||
} | ||
let url = self.iidURL + "batchRemove" | ||
return getAccessToken().flatMapThrowing { accessToken throws -> HTTPClient.Request in | ||
struct Payload: Codable { | ||
let to: String | ||
let registration_tokens: [String] | ||
|
||
init (to: String, registration_tokens: [String]) { | ||
self.to = "/topics/\(to)" | ||
self.registration_tokens = registration_tokens | ||
} | ||
} | ||
let payload = Payload(to: name, registration_tokens: tokens) | ||
let payloadData = try JSONEncoder().encode(payload) | ||
var headers = HTTPHeaders() | ||
headers.add(name: "Authorization", value: "key=\(serverKey)") | ||
headers.add(name: "Content-Type", value: "application/json") | ||
return try .init(url: url, method: .POST, headers: headers, body: .data(payloadData)) | ||
}.flatMap { request in | ||
return self.client.execute(request: request).flatMapThrowing { res in | ||
guard 200 ..< 300 ~= res.status.code else { | ||
if let body = res.body, let googleError = try? JSONDecoder().decode(GoogleError.self, from: body) { | ||
throw googleError | ||
} else { | ||
guard | ||
let bb = res.body, | ||
let bytes = bb.getBytes(at: 0, length: bb.readableBytes), | ||
let reason = String(bytes: bytes, encoding: .utf8) else { | ||
throw Abort(.internalServerError, reason: "FCM: DeleteTopic: unable to decode error response") | ||
} | ||
throw Abort(.internalServerError, reason: reason) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |