passforios/passKit/Helpers/NotificationCenterDispatcher.swift
Danny Mösch 3eea063d61
Fix issues with OTP notification (#517)
* Use "Beta" since this is what Apple uses too
* Actually copy the OTP if the option is set
* Shorten notification text to make it fit to smaller screens
* Set notification center delegate before app launches
* Fix SwiftFormat issue fixed with version 0.48.12
2021-10-03 17:20:22 -07:00

48 lines
1.9 KiB
Swift

//
// NotificationCenterDispatcher.swift
// passKit
//
// Created by Danny Moesch on 29.09.21.
// Copyright © 2021 Bob Sun. All rights reserved.
//
public class NotificationCenterDispatcher: NSObject, UNUserNotificationCenterDelegate {
public static let shared = NotificationCenterDispatcher()
public func userNotificationCenter(_: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if response.actionIdentifier == Globals.otpNotificationCopyAction {
if let otp = response.notification.request.content.userInfo["otp"] as? String {
UIPasteboard.general.string = otp
}
}
completionHandler()
}
public static func showOTPNotification(password: Password) {
guard let otp = password.currentOtp else {
return
}
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.getNotificationSettings { state in
guard state.authorizationStatus == .authorized else {
return
}
let content = UNMutableNotificationContent()
content.title = "OTPForPassword".localize(password.name)
if Defaults.autoCopyOTP {
content.body = "OTPHasBeenCopied".localize()
UIPasteboard.general.string = otp
} else {
content.body = otp
content.categoryIdentifier = Globals.otpNotificationCategory
content.userInfo = [
"path": password.namePath,
"otp": otp,
]
}
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
let request = UNNotificationRequest(identifier: Globals.otpNotification, content: content, trigger: trigger)
notificationCenter.add(request)
}
}
}