从通知重定向到另一个故事板?
Redirect to another StoryBoard from Notification?
默认情况下,我们从通知中打开 Mail.Storyboard 的第一个 ViewController。是否可以打开另一个故事板?我已经尝试了一些解决方案,但对我不起作用。整个代码如下。如果需要更多信息,请告诉我。非常感谢。
import UIKit
import UserNotifications
class Notifications: UIViewController, UNUserNotificationCenterDelegate {
@IBOutlet weak var scheludeMeal: UIButton!
@IBOutlet weak var firstDatePicker: UIDatePicker!
...
var savedFirstNotification: Date? {
get {
return UserDefaults.standard.object(forKey: "firstMealTime") as? Date
}
set {
UserDefaults.standard.set(newValue, forKey: "firstMealTime")
}
}
...
}
override func viewDidLoad() {
super.viewDidLoad()
turnOnNotification.isHidden = true
if let savedFirstNotification = savedFirstNotification {
firstDatePicker.date = savedFirstNotification
}
...
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
if granted {
print("Yes!")
} else {
print("No...?")
}
}
}
...
@IBAction func scheludeButton(_ sender: Any) {
let center = UNUserNotificationCenter.current()
center.removeAllPendingNotificationRequests()
let content = UNMutableNotificationContent()
content.title = "Ttile"
content.body = "Description of notification :)"
content.sound = UNNotificationSound.default
let firstMealComponent = firstDatePicker.calendar.dateComponents([.hour, .minute], from: firstDatePicker.date)
let trigger = UNCalendarNotificationTrigger(dateMatching: firstMealComponent, repeats: true)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
center.add(request)
savedFirstNotification = firstDatePicker.date
...
}
}
这是您正在寻找的解决方案的粗略版本,当然您可以添加更多验证,例如根据通知负载(userInfo)您可以转到不同的控制器。
extension AppDelegate: UNUserNotificationCenterDelegate {
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge, .sound])
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let storyboard = UIStoryboard(name: "yourStoryboardName", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "yourControllerName")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
@available(iOS 9, *)
func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [AnyHashable : Any], withResponseInfo responseInfo: [AnyHashable : Any], completionHandler: @escaping () -> Void) {
let storyboard = UIStoryboard(name: "yourStoryboardName", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "yourControllerName")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
completionHandler()
}
}
默认情况下,我们从通知中打开 Mail.Storyboard 的第一个 ViewController。是否可以打开另一个故事板?我已经尝试了一些解决方案,但对我不起作用。整个代码如下。如果需要更多信息,请告诉我。非常感谢。
import UIKit
import UserNotifications
class Notifications: UIViewController, UNUserNotificationCenterDelegate {
@IBOutlet weak var scheludeMeal: UIButton!
@IBOutlet weak var firstDatePicker: UIDatePicker!
...
var savedFirstNotification: Date? {
get {
return UserDefaults.standard.object(forKey: "firstMealTime") as? Date
}
set {
UserDefaults.standard.set(newValue, forKey: "firstMealTime")
}
}
...
}
override func viewDidLoad() {
super.viewDidLoad()
turnOnNotification.isHidden = true
if let savedFirstNotification = savedFirstNotification {
firstDatePicker.date = savedFirstNotification
}
...
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
if granted {
print("Yes!")
} else {
print("No...?")
}
}
}
...
@IBAction func scheludeButton(_ sender: Any) {
let center = UNUserNotificationCenter.current()
center.removeAllPendingNotificationRequests()
let content = UNMutableNotificationContent()
content.title = "Ttile"
content.body = "Description of notification :)"
content.sound = UNNotificationSound.default
let firstMealComponent = firstDatePicker.calendar.dateComponents([.hour, .minute], from: firstDatePicker.date)
let trigger = UNCalendarNotificationTrigger(dateMatching: firstMealComponent, repeats: true)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
center.add(request)
savedFirstNotification = firstDatePicker.date
...
}
}
这是您正在寻找的解决方案的粗略版本,当然您可以添加更多验证,例如根据通知负载(userInfo)您可以转到不同的控制器。
extension AppDelegate: UNUserNotificationCenterDelegate {
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge, .sound])
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let storyboard = UIStoryboard(name: "yourStoryboardName", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "yourControllerName")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
@available(iOS 9, *)
func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [AnyHashable : Any], withResponseInfo responseInfo: [AnyHashable : Any], completionHandler: @escaping () -> Void) {
let storyboard = UIStoryboard(name: "yourStoryboardName", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "yourControllerName")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
completionHandler()
}
}