Skip to content

Commit

Permalink
Merge pull request #1914 from nextcloud/feat/noid/test-push-notificat…
Browse files Browse the repository at this point in the history
…ions

feat(PushNotifications): Allow to test push notifications
  • Loading branch information
SystemKeeper authored Dec 12, 2024
2 parents 6d78cf2 + cb99131 commit 4893dfe
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 14 deletions.
103 changes: 89 additions & 14 deletions NextcloudTalk/DiagnosticsTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ class DiagnosticsTableViewController: UITableViewController {
}

enum AccountSections: Int {
case kAccountSectionServer = 0
case kAccountSectionUser
case kAccountPushSubscribed
case kAccountSectionCount
case server = 0
case user
case pushSubscribed
case testPushNotifications
}

enum ServerSections: Int {
Expand Down Expand Up @@ -79,6 +79,8 @@ class DiagnosticsTableViewController: UITableViewController {
var serverReachable: Bool?
var serverReachableIndicator = UIActivityIndicatorView(frame: .init(x: 0, y: 0, width: 24, height: 24))

var testingPushNotificationsIndicator = UIActivityIndicatorView(frame: .init(x: 0, y: 0, width: 24, height: 24))

var notificationSettings: UNNotificationSettings?
var notificationSettingsIndicator = UIActivityIndicatorView(frame: .init(x: 0, y: 0, width: 24, height: 24))

Expand All @@ -87,7 +89,7 @@ class DiagnosticsTableViewController: UITableViewController {
let notRequestedString = NSLocalizedString("Not requested", comment: "'{Microphone, Camera, ...} access was not requested'")
let deniedFunctionalityString = NSLocalizedString("This will impact the functionality of this app. Please review your settings.", comment: "")

let cellIdentifierOpenAppSettings = "cellIdentifierOpenAppSettings"
let cellIdentifierAction = "cellIdentifierAction"
let cellIdentifierSubtitle = "cellIdentifierSubtitle"
let cellIdentifierSubtitleAccessory = "cellIdentifierSubtitleAccessory"

Expand Down Expand Up @@ -133,13 +135,25 @@ class DiagnosticsTableViewController: UITableViewController {
self.navigationItem.compactAppearance = appearance
self.navigationItem.scrollEdgeAppearance = appearance

self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellIdentifierOpenAppSettings)
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellIdentifierAction)
self.tableView.register(SubtitleTableViewCell.self, forCellReuseIdentifier: cellIdentifierSubtitle)
self.tableView.register(SubtitleTableViewCell.self, forCellReuseIdentifier: cellIdentifierSubtitleAccessory)

runChecks()
}

// MARK: - Account section options

func accountSections() -> [AccountSections] {
var sections: [AccountSections] = [.server, .user, .pushSubscribed]
if NCDatabaseManager.sharedInstance().serverHasNotificationsCapability(kNotificationsCapabilityTestPush, forAccountId: account.accountId) {
sections.append(.testPushNotifications)
}

return sections
}


// MARK: Async. checks

func runChecks() {
Expand Down Expand Up @@ -191,7 +205,7 @@ class DiagnosticsTableViewController: UITableViewController {
return AppSections.kAppSectionCount.rawValue

case DiagnosticsSections.kDiagnosticsSectionAccount.rawValue:
return AccountSections.kAccountSectionCount.rawValue
return accountSections().count

case DiagnosticsSections.kDiagnosticsSectionServer.rawValue:
return ServerSections.kServerSectionCount.rawValue
Expand Down Expand Up @@ -259,6 +273,11 @@ class DiagnosticsTableViewController: UITableViewController {

UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)

} else if indexPath.section == DiagnosticsSections.kDiagnosticsSectionAccount.rawValue,
accountSections()[indexPath.row] == .testPushNotifications {

testPushNotifications()

} else if indexPath.section == DiagnosticsSections.kDiagnosticsSectionTalk.rawValue,
indexPath.row == TalkSections.kTalkSectionVersion.rawValue {

Expand Down Expand Up @@ -307,7 +326,7 @@ class DiagnosticsTableViewController: UITableViewController {
return appPhotoLibraryAccessCell(for: indexPath)

case AppSections.kAppSectionOpenSettings.rawValue:
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifierOpenAppSettings, for: indexPath)
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifierAction, for: indexPath)

cell.textLabel?.text = NSLocalizedString("Open app settings", comment: "")
cell.textLabel?.textAlignment = .center
Expand Down Expand Up @@ -452,17 +471,18 @@ class DiagnosticsTableViewController: UITableViewController {

func accountCell(for indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifierSubtitle, for: indexPath)
let row: AccountSections = accountSections()[indexPath.row]

switch indexPath.row {
case AccountSections.kAccountSectionServer.rawValue:
switch row {
case .server:
cell.textLabel?.text = NSLocalizedString("Server", comment: "")
cell.detailTextLabel?.text = account.server

case AccountSections.kAccountSectionUser.rawValue:
case .user:
cell.textLabel?.text = NSLocalizedString("User", comment: "")
cell.detailTextLabel?.text = account.user

case AccountSections.kAccountPushSubscribed.rawValue:
case .pushSubscribed:
cell.textLabel?.text = NSLocalizedString("Push notifications", comment: "")
if account.lastPushSubscription > 0 {
let lastSubsctiptionString = NSLocalizedString("Last subscription", comment: "Last subscription to the push notification server")
Expand All @@ -472,8 +492,12 @@ class DiagnosticsTableViewController: UITableViewController {
cell.detailTextLabel?.text = NSLocalizedString("Never subscribed", comment: "Never subscribed to the push notification server")
}

default:
break
case .testPushNotifications:
let actionCell = self.tableView.dequeueReusableCell(withIdentifier: self.cellIdentifierAction, for: indexPath)
actionCell.textLabel?.text = NSLocalizedString("Test push notifications", comment: "")
actionCell.textLabel?.textAlignment = .center
actionCell.textLabel?.textColor = UIColor.systemBlue
return actionCell
}

return cell
Expand Down Expand Up @@ -623,6 +647,57 @@ class DiagnosticsTableViewController: UITableViewController {
return cell
}

// MARK: Test push notifications

func testPushNotifications() {
self.showPushNotificationTestRunningIndicator()
NCAPIController.sharedInstance().testPushnotifications(forAccount: account) { result in
let isEmptyResult = result?.isEmpty ?? true
let title = isEmptyResult ? NSLocalizedString("Test failed", comment: "") : NSLocalizedString("Test results", comment: "")
let message = isEmptyResult ? NSLocalizedString("An error occurred while testing push notifications", comment: "") : result
let alert = UIAlertController(title: title,
message: message,
preferredStyle: .alert)

alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default))
if !isEmptyResult {
alert.addAction(UIAlertAction(title: NSLocalizedString("Copy", comment: ""), style: .default) { _ in
UIPasteboard.general.string = result
NotificationPresenter.shared().present(text: NSLocalizedString("Test results copied", comment: ""), dismissAfterDelay: 5.0, includedStyle: .dark)
})
}
NCUserInterfaceController.sharedInstance().presentAlertViewController(alert)
self.hidePushNotificationTestRunningIndicator()
}
}

func testPushNotificationsCell() -> UITableViewCell? {
if let index = accountSections().firstIndex(of: AccountSections.testPushNotifications),
let testPushCell = tableView.cellForRow(at: IndexPath(row: index, section: DiagnosticsSections.kDiagnosticsSectionAccount.rawValue)) {
return testPushCell
}

return nil
}

func showPushNotificationTestRunningIndicator() {
if let testPushNotificationsCell = testPushNotificationsCell() {
testPushNotificationsCell.isUserInteractionEnabled = false
testPushNotificationsCell.textLabel?.textColor = UIColor.systemBlue.withAlphaComponent(0.5)
testingPushNotificationsIndicator.startAnimating()
testPushNotificationsCell.accessoryView = testingPushNotificationsIndicator
}
}

func hidePushNotificationTestRunningIndicator() {
if let testPushNotificationsCell = testPushNotificationsCell() {
testPushNotificationsCell.isUserInteractionEnabled = true
testPushNotificationsCell.textLabel?.textColor = UIColor.systemBlue
testingPushNotificationsIndicator.stopAnimating()
testPushNotificationsCell.accessoryView = nil
}
}

// MARK: Capabilities details

func presentCapabilitiesDetails() {
Expand Down
17 changes: 17 additions & 0 deletions NextcloudTalk/NCAPIControllerExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -559,4 +559,21 @@ import Foundation
completionBlock(error == nil)
}
}

// MARK: - Push notification test

public func testPushnotifications(forAccount account: TalkAccount, completionBlock: @escaping (_ result: String?) -> Void) {
guard let apiSessionManager = self.apiSessionManagers.object(forKey: account.accountId) as? NCAPISessionManager
else {
completionBlock(nil)
return
}

let urlString = "\(account.server)/ocs/v2.php/apps/notifications/api/v3/test/self"

apiSessionManager.postOcs(urlString, account: account) { ocsResponse, ocsError in
let message = ocsResponse?.dataDict?["message"] as? String
completionBlock(message)
}
}
}
1 change: 1 addition & 0 deletions NextcloudTalk/NCDatabaseManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ extern NSString * const kCapabilityChatSummary;
extern NSString * const kCapabilityArchivedConversationsV2;

extern NSString * const kNotificationsCapabilityExists;
extern NSString * const kNotificationsCapabilityTestPush;

extern NSString * const kMinimumRequiredTalkCapability;

Expand Down
1 change: 1 addition & 0 deletions NextcloudTalk/NCDatabaseManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
NSString * const kCapabilityArchivedConversationsV2 = @"archived-conversations-v2";

NSString * const kNotificationsCapabilityExists = @"exists";
NSString * const kNotificationsCapabilityTestPush = @"test-push";

NSString * const kMinimumRequiredTalkCapability = kCapabilitySystemMessages; // Talk 4.0 is the minimum required version

Expand Down
15 changes: 15 additions & 0 deletions NextcloudTalk/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@
/* No comment provided by engineer. */
"An error occurred while sharing the file" = "An error occurred while sharing the file";

/* No comment provided by engineer. */
"An error occurred while testing push notifications" = "An error occurred while testing push notifications";

/* No comment provided by engineer. */
"an hour" = "an hour";

Expand Down Expand Up @@ -1726,6 +1729,18 @@
/* No comment provided by engineer. */
"Tap and hold to record a voice message, release the button to send it." = "Tap and hold to record a voice message, release the button to send it.";

/* No comment provided by engineer. */
"Test failed" = "Test failed";

/* No comment provided by engineer. */
"Test push notifications" = "Test push notifications";

/* No comment provided by engineer. */
"Test results" = "Test results";

/* No comment provided by engineer. */
"Test results copied" = "Test results copied";

/* No comment provided by engineer. */
"The app is too old and no longer supported by this server." = "The app is too old and no longer supported by this server.";

Expand Down

0 comments on commit 4893dfe

Please sign in to comment.