Initial add test notification

This commit is contained in:
Andrew Cope 2022-02-19 11:42:32 -05:00
parent f79eb4731b
commit 954c773500
3 changed files with 31 additions and 1 deletions

View file

@ -10,7 +10,7 @@ This document is to keep track of the feature parity between the iOS and Android
| Subscribe to self-hosted server topic | :x: | :white_check_mark: | Not yet implemented |
| Instant delivery | :x: | :white_check_mark: | Foreground services not possible in iOS |
| Pause notifications | :x: | :white_check_mark: | Will likely require [Filtering](https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_developer_usernotifications_filtering) to prevent displaying notifications while still receiving them |
| Send test notification | :x: | :white_check_mark: | Not yet implemented |
| Send test notification | :warning: | :white_check_mark: | Not fully implemented |
| Unsubscribe from topic | :white_check_mark: | :white_check_mark: |
| Delete notifications | :x: | :white_check_mark: | Not yet implemented |
| Notification priority | :warning: | :white_check_mark: | Displays an exclamation mark in notification row for high.max priority, no changes to the actual push notification (sounds, vibrations), no prioirty filtering |

View file

@ -17,6 +17,21 @@ class ApiService {
fetchJsonData(urlString: urlString, completionHandler: completionHandler)
}
func publish(subscription: NtfySubscription, message: String, title: String, priority: Int = 3, tags: [String] = [], completionHandler: @escaping (NtfyNotification?, Error?) -> Void) {
guard let url = URL(string: subscription.urlString()) else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue(title, forHTTPHeaderField: "Title")
request.setValue(String(priority), forHTTPHeaderField: "Priority")
request.setValue(tags.joined(separator: ","), forHTTPHeaderField: "Tags")
request.httpBody = message.data(using: String.Encoding.utf8)
URLSession.shared.dataTask(with: request) { (data, response, error) in
print(data)
print(response)
print(error)
}.resume()
}
private func fetchJsonData<T: Decodable>(urlString: String, completionHandler: @escaping ([T]?, Error?) -> ()) {
guard let url = URL(string: urlString) else { return }
URLSession.shared.dataTask(with: url) { (data, response, error) in

View file

@ -23,6 +23,21 @@ struct SubscriptionDetail: View {
ToolbarItem(placement: .principal) {
Text(subscription.displayName()).font(.headline)
}
ToolbarItem(placement: .navigationBarTrailing) {
Menu("Edit") {
Button("Send Test Notification") {
let priority = Int.random(in: 1..<6)
ApiService.shared.publish(
subscription: subscription,
message: "This is a test notification from the Ntfy iOS app. It has a priority of \(priority).",
title: "Test: You can set a title if you like",
priority: priority
) { _,_ in
print("Success")
}
}
}
}
}
.overlay(Group {
if notifications.isEmpty {