Merge pull request #2085 from stuartbreckenridge/2057

2057 - Notification permissions are only requested when the user asks to be Notified of New Articles
This commit is contained in:
Maurice Parker 2020-05-18 02:03:47 -05:00 committed by GitHub
commit 4b1c40f264
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 4 deletions

View File

@ -91,8 +91,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
self.unreadCount = AccountManager.shared.unreadCount self.unreadCount = AccountManager.shared.unreadCount
} }
UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .sound, .alert]) { (granted, error) in UNUserNotificationCenter.current().getNotificationSettings { (settings) in
if granted { if settings.authorizationStatus == .authorized {
DispatchQueue.main.async { DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications() UIApplication.shared.registerForRemoteNotifications()
} }

View File

@ -9,6 +9,7 @@
import UIKit import UIKit
import Account import Account
import SafariServices import SafariServices
import UserNotifications
class WebFeedInspectorViewController: UITableViewController { class WebFeedInspectorViewController: UITableViewController {
@ -38,6 +39,8 @@ class WebFeedInspectorViewController: UITableViewController {
return webFeed.homePageURL == nil return webFeed.homePageURL == nil
} }
private var userNotificationSettings: UNNotificationSettings?
override func viewDidLoad() { override func viewDidLoad() {
tableView.register(InspectorIconHeaderView.self, forHeaderFooterViewReuseIdentifier: "SectionHeader") tableView.register(InspectorIconHeaderView.self, forHeaderFooterViewReuseIdentifier: "SectionHeader")
@ -51,6 +54,13 @@ class WebFeedInspectorViewController: UITableViewController {
feedURLLabel.text = webFeed.url feedURLLabel.text = webFeed.url
NotificationCenter.default.addObserver(self, selector: #selector(webFeedIconDidBecomeAvailable(_:)), name: .WebFeedIconDidBecomeAvailable, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(webFeedIconDidBecomeAvailable(_:)), name: .WebFeedIconDidBecomeAvailable, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(updateNotificationSettings), name: UIApplication.willEnterForegroundNotification, object: nil)
}
override func viewDidAppear(_ animated: Bool) {
updateNotificationSettings()
} }
override func viewDidDisappear(_ animated: Bool) { override func viewDidDisappear(_ animated: Bool) {
@ -67,7 +77,30 @@ class WebFeedInspectorViewController: UITableViewController {
} }
@IBAction func notifyAboutNewArticlesChanged(_ sender: Any) { @IBAction func notifyAboutNewArticlesChanged(_ sender: Any) {
webFeed.isNotifyAboutNewArticles = notifyAboutNewArticlesSwitch.isOn guard let settings = userNotificationSettings else {
notifyAboutNewArticlesSwitch.isOn = !notifyAboutNewArticlesSwitch.isOn
return
}
if settings.authorizationStatus == .denied {
notifyAboutNewArticlesSwitch.isOn = !notifyAboutNewArticlesSwitch.isOn
present(notificationUpdateErrorAlert(), animated: true, completion: nil)
} else if settings.authorizationStatus == .authorized {
webFeed.isNotifyAboutNewArticles = notifyAboutNewArticlesSwitch.isOn
} else {
UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .sound, .alert]) { (granted, error) in
self.updateNotificationSettings()
if granted {
DispatchQueue.main.async {
self.webFeed.isNotifyAboutNewArticles = self.notifyAboutNewArticlesSwitch.isOn
UIApplication.shared.registerForRemoteNotifications()
}
} else {
DispatchQueue.main.async {
self.notifyAboutNewArticlesSwitch.isOn = !self.notifyAboutNewArticlesSwitch.isOn
}
}
}
}
} }
@IBAction func alwaysShowReaderViewChanged(_ sender: Any) { @IBAction func alwaysShowReaderViewChanged(_ sender: Any) {
@ -158,3 +191,33 @@ extension WebFeedInspectorViewController: UITextFieldDelegate {
} }
} }
// MARK: UNUserNotificationCenter
extension WebFeedInspectorViewController {
@objc
func updateNotificationSettings() {
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
DispatchQueue.main.async {
self.userNotificationSettings = settings
if settings.authorizationStatus == .authorized {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
}
func notificationUpdateErrorAlert() -> UIAlertController {
let alert = UIAlertController(title: NSLocalizedString("Enable Notifications", comment: "Notifications"),
message: NSLocalizedString("Notifications need to be enabled in the Settings app.", comment: "Notifications need to be enabled in the Settings app."), preferredStyle: .alert)
let openSettings = UIAlertAction(title: NSLocalizedString("Open Settings", comment: "Open Settings"), style: .default) { (action) in
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [UIApplication.OpenExternalURLOptionsKey.universalLinksOnly : false], completionHandler: nil)
}
let dismiss = UIAlertAction(title: NSLocalizedString("Dismiss", comment: "Dismiss"), style: .cancel, handler: nil)
alert.addAction(openSettings)
alert.addAction(dismiss)
return alert
}
}