当我点击横幅时,我的应用程序打开但没有当前的VC,我需要打开NotificationsVC。在其他VC中,我使用RXSwift。我在哪里可以添加动作?
enum PushNotificationAction {
case show
case open
}
class PushManagerNotification {
func convert(with value: [AnyHashable: Any], and state: PushNotificationAction) {
...
guard let messageBody = dictionary["msg"] as? [String: AnyObject] else { return }
if state == .show {
showBadges(dictionary: messageBody)
return
}
switch notificationType {
case .notification:
showNotification(dictionary: messageBody)
break
case .news:
showNewsNotification(dictionary: messageBody)
break
default:
break;
}
}
private func showNotification(dictionary: [String:AnyObject]) {
if let message = NotificationEntity(JSON: dictionary) {
NotificationCenter.default.post(name: .notification, object: message);
}
}
private func showNewsNotification(dictionary: [String:AnyObject]) {
if let message = NewsNotificationEntity(JSON: dictionary) {
NotificationCenter.default.post(name: .news, object: message);
}
}
}
在AppDelegate中
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
pushManagerNotification.convert(with: userInfo, and: .open)
completionHandler()
}
在didFinishLaunchingWithOptions中:
if let options = launchOptions, let remotePush = options[UIApplication.LaunchOptionsKey.remoteNotification], let accessToken = KeychainSwift().get(Constants.accessToken),let refreshToken = KeychainSwift().get(Constants.refreshToken) {
let isLogin = UserDefaults.standard.bool(forKey: Constants.isLogin);
if isLogin && !accessToken.isEmpty && !refreshToken.isEmpty{
if let value = remotePush as? [AnyHashable:Any] {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) { [weak self] in
self?.pushManagerNotification.convert(with: value, and: .open);
}
}
}
}