First version with app notifications

This commit is contained in:
relikd
2020-07-26 22:32:11 +02:00
parent 88a52fb92c
commit a2b0f311d5
37 changed files with 2192 additions and 469 deletions

View File

@@ -11,6 +11,9 @@ class TBCMain: UITabBarController {
if !Prefs.DidShowTutorial.Welcome {
self.perform(#selector(showWelcomeMessage), with: nil, afterDelay: 0.5)
}
if #available(iOS 10.0, *) {
initNotifications()
}
}
@objc private func reloadTabBarBadge() {
@@ -57,3 +60,63 @@ class TBCMain: UITabBarController {
}
}
}
extension TBCMain {
@discardableResult func openTab(_ index: Int) -> UIViewController? {
selectedIndex = index
guard let nav = selectedViewController as? UINavigationController else {
return selectedViewController
}
nav.popToRootViewController(animated: false)
return nav.topViewController
}
}
// MARK: - Push Notifications
@available(iOS 10.0, *)
extension TBCMain: UNUserNotificationCenterDelegate {
func initNotifications() {
UNUserNotificationCenter.current().delegate = self
guard Prefs.RecordingReminder.Enabled else {
return
}
PushNotification.allowed {
switch $0 {
case .NotDetermined:
PushNotification.requestProvisionalOrDoNothing { success in
guard success else { return }
PushNotification.scheduleRecordingReminder(force: false)
}
case .Denied:
break
case .Authorized, .Provisional:
PushNotification.scheduleRecordingReminder(force: false)
}
}
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge, .sound]) // in-app notifications
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
switch response.notification.request.identifier {
case PushNotification.Identifier.YouShallRecordMoreReminder.rawValue:
selectedIndex = 1 // open recordings tab
case PushNotification.Identifier.CantStopMeNowReminder.rawValue:
(openTab(2) as! TVCSettings).openRestartVPNSettings()
//case PushNotification.Identifier.RestInPeaceTombstoneReminder // only badge
default: // domain notification
// TODO: open specific domain?
openTab(0) // open Requests tab
}
completionHandler()
}
@available(iOS 12.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification?) {
(openTab(2) as! TVCSettings).openNotificationSettings()
}
}