Random messages

This commit is contained in:
Christoph Hagen 2022-09-27 21:38:16 +02:00
parent c19bc3c299
commit ec309e77db
2 changed files with 18 additions and 14 deletions

View File

@ -8,7 +8,9 @@ struct APNSInterface {
private struct Payload: Codable {}
private var apnsConfiguration: APNSClientConfiguration!
private var apnsNotification: APNSAlertNotification<Payload>!
private let title: String
private let messages: [String]
private let topic: String
private let apnsEventGroup = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
private let apnsRequestEncoder = JSONEncoder()
private let apnsResponseDecoder = JSONDecoder()
@ -26,18 +28,9 @@ struct APNSInterface {
apnsConfiguration = APNSClientConfiguration(
authenticationMethod: authentication,
environment: .sandbox)
let alert = APNSAlertNotificationContent(
title: .raw(config.messageTitle),
body: .raw(config.messageBody))
apnsNotification = APNSAlertNotification(
alert: alert,
expiration: .none,
priority: .immediately,
topic: config.topic,
payload: Payload(),
sound: .default)
title = config.messageTitle
messages = config.messages
topic = config.topic
}
func sendPush(to tokens: Set<String>) async {
@ -47,6 +40,17 @@ struct APNSInterface {
responseDecoder: apnsResponseDecoder,
requestEncoder: apnsRequestEncoder)
log(info: "Client created")
let alert = APNSAlertNotificationContent(
title: .raw(title),
body: .raw(messages.randomElement()!))
let apnsNotification = APNSAlertNotification(
alert: alert,
expiration: .none,
priority: .immediately,
topic: topic,
payload: Payload(),
sound: .default)
do {
for token in tokenStorage.tokens {
log(info: "Sending push to \(token.prefix(6))...")

View File

@ -16,7 +16,7 @@ struct ServerConfiguration: Codable {
let messageTitle: String
let messageBody: String
let messages: [String]
let buttonPin: Int