Add push notifications
This commit is contained in:
105
CHDataManagement/Notifications/NotificationSheet.swift
Normal file
105
CHDataManagement/Notifications/NotificationSheet.swift
Normal file
@@ -0,0 +1,105 @@
|
||||
import SwiftUI
|
||||
import SFSafeSymbols
|
||||
|
||||
struct NotificationSheet: View {
|
||||
|
||||
@EnvironmentObject
|
||||
private var content: Content
|
||||
|
||||
@EnvironmentObject
|
||||
private var notifications: NotificationSender
|
||||
|
||||
@Environment(\.dismiss)
|
||||
private var dismiss
|
||||
|
||||
@State
|
||||
private var englishTitle = ""
|
||||
|
||||
@State
|
||||
private var englishMessage = ""
|
||||
|
||||
@State
|
||||
private var germanTitle = ""
|
||||
|
||||
@State
|
||||
private var germanMessage = ""
|
||||
|
||||
private var uploadSymbol: SFSymbol {
|
||||
if notifications.isTransmittingToRemote {
|
||||
return .squareAndArrowUpBadgeClock
|
||||
}
|
||||
if !notifications.lastPushWasSuccessful {
|
||||
return .squareAndArrowUpTrianglebadgeExclamationmark
|
||||
}
|
||||
return .squareAndArrowUp
|
||||
}
|
||||
|
||||
var header: String {
|
||||
let user = content.settings.general.remoteUserForUpload
|
||||
let port = content.settings.general.remotePortForUpload
|
||||
let url = content.settings.general.remotePathForUpload.withHttpPrefixRemoved
|
||||
let path = content.settings.general.remotePathForUpload
|
||||
return "\(user)@\(url):\(port)/\(path)"
|
||||
}
|
||||
|
||||
var isIncomplete: Bool {
|
||||
englishTitle.isEmpty || germanTitle.isEmpty || englishMessage.isEmpty || germanMessage.isEmpty
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
HStack {
|
||||
Text("Send a notification to subscribers")
|
||||
.font(.headline)
|
||||
Spacer()
|
||||
Button("Close", action: { dismiss() })
|
||||
}
|
||||
StringPropertyView(
|
||||
title: "English title",
|
||||
text: $englishTitle,
|
||||
footer: "The title for devices using the english language")
|
||||
|
||||
StringPropertyView(
|
||||
title: "English message",
|
||||
text: $englishMessage,
|
||||
footer: "The message for devices using the english language")
|
||||
|
||||
StringPropertyView(
|
||||
title: "German title",
|
||||
text: $germanTitle,
|
||||
footer: "The title for devices using the german language")
|
||||
|
||||
StringPropertyView(
|
||||
title: "German message",
|
||||
text: $germanMessage,
|
||||
footer: "The message for devices using the german language")
|
||||
|
||||
Button("Send", action: send)
|
||||
.disabled(notifications.isTransmittingToRemote || isIncomplete)
|
||||
}
|
||||
.padding()
|
||||
.frame(minWidth: 300, idealWidth: 400, idealHeight: 500)
|
||||
}
|
||||
|
||||
private func send() {
|
||||
guard let accessToken = content.settings.general.pushNotificationAccessToken,
|
||||
let url = content.settings.general.urlForPushNotification else {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
let english = WebNotification(title: englishTitle, body: englishMessage)
|
||||
let german = WebNotification(title: germanTitle, body: germanMessage)
|
||||
|
||||
let notifications: [String : WebNotification] = [
|
||||
"en": english,
|
||||
"de": german
|
||||
]
|
||||
|
||||
let request = NotificationRequest.init(
|
||||
accessToken: accessToken,
|
||||
notifications: notifications)
|
||||
|
||||
self.notifications.send(url: url, request: request)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user