我用Swift4 Xcode 9构建了一个包含推式通知的应用程序,我希望当我单击推式通知时它将移动到某个viewController。例如,有viewControllerA,viewControllerB,viewControllerC。当单击通知A时,它将转到viewControllerA。我该怎么做。我刚学过Swift。请帮忙,谢谢。
AppDelegate.swift
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let notifIdentifier = response.notification.request.identifier
print("Notification identifier: \(notifIdentifier)")
switch response.actionIdentifier {
case "open":
print("Open")
self.showCashout()
completionHandler()
default:
completionHandler()
}
completionHandler()
}
func showCashout() {
let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "cashoutVC") as! CashoutController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController?.present(initialViewController, animated: true, completion: nil)
}
func createNotification(title: String, body: String) {
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: title, arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: body, arguments: nil)
content.sound = UNNotificationSound.default()
content.badge = NSNumber(integerLiteral: UIApplication.shared.applicationIconBadgeNumber + 1)
content.categoryIdentifier = "Category"
let request = UNNotificationRequest.init(identifier: "PushNotif", content: content, trigger: nil)
let center = UNUserNotificationCenter.current()
let openAction = UNNotificationAction.init(identifier: "open", title: "Open", options: UNNotificationActionOptions.foreground)
let categories = UNNotificationCategory.init(identifier: "Category", actions: [openAction], intentIdentifiers: [], options: [])
center.setNotificationCategories([categories])
center.add(request)
}