From 954c773500a28209c4527d50ff322358ca56b6f8 Mon Sep 17 00:00:00 2001 From: Andrew Cope Date: Sat, 19 Feb 2022 11:42:32 -0500 Subject: [PATCH] Initial add test notification --- docs/FEATURE_PARITY.md | 2 +- ntfy-ios/Utils/ApiService.swift | 15 +++++++++++++++ ntfy-ios/Views/SubscriptionDetail.swift | 15 +++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/docs/FEATURE_PARITY.md b/docs/FEATURE_PARITY.md index 1059893..741fedd 100644 --- a/docs/FEATURE_PARITY.md +++ b/docs/FEATURE_PARITY.md @@ -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 | diff --git a/ntfy-ios/Utils/ApiService.swift b/ntfy-ios/Utils/ApiService.swift index 2897030..aff6dee 100644 --- a/ntfy-ios/Utils/ApiService.swift +++ b/ntfy-ios/Utils/ApiService.swift @@ -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(urlString: String, completionHandler: @escaping ([T]?, Error?) -> ()) { guard let url = URL(string: urlString) else { return } URLSession.shared.dataTask(with: url) { (data, response, error) in diff --git a/ntfy-ios/Views/SubscriptionDetail.swift b/ntfy-ios/Views/SubscriptionDetail.swift index aeb67cf..22506f6 100644 --- a/ntfy-ios/Views/SubscriptionDetail.swift +++ b/ntfy-ios/Views/SubscriptionDetail.swift @@ -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 {