当用户点击通知时从 UNNotificationRequest 显示 ViewController
Present ViewController from UNNotificationRequest when user taps on notification
我目前正在 Swift 4 中开发一个包含本地通知的应用程序。当用户点击通知时,我试图 运行 一段特定的代码,比如呈现特定的 ViewController。我找不到关于堆栈溢出的答案,因为 App delegate 中的大多数函数都被弃用了。
这是我用于通知的代码:
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = "Consejo"
content.body = "Haga clic en la notificacion para ver el consejo del dia."
content.sound = UNNotificationSound.default()
var dateComponents = DateComponents()
if let hour = pickerdatehour {
if let minute = pickerdateminute{
dateComponents.hour = hour
dateComponents.minute = minute
dateComponents.second = 0
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
center.add(request, withCompletionHandler: nil)
我试图从应用程序委托执行的代码是:
import UIKit
import Firebase
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("tes")
let sb = UIStoryboard(name: "Main", bundle: nil)
let otherVC = sb.instantiateViewController(withIdentifier: "consejos") as! ConsejosViewController
window?.rootViewController = otherVC;
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("tes")
let sb = UIStoryboard(name: "Main", bundle: nil)
let otherVC = sb.instantiateViewController(withIdentifier: "consejos") as! ConsejosViewController
window?.rootViewController = otherVC;
}
}
我做错了什么? app delegate 上的代码甚至从未执行过。
感谢任何帮助
您需要将 UNUserNotificationCenter
委托设置为自己来调用 UNUserNotificationCenter
的委托方法,如下所示:
let center = UNUserNotificationCenter.current()
center.delegate = self
我目前正在 Swift 4 中开发一个包含本地通知的应用程序。当用户点击通知时,我试图 运行 一段特定的代码,比如呈现特定的 ViewController。我找不到关于堆栈溢出的答案,因为 App delegate 中的大多数函数都被弃用了。
这是我用于通知的代码:
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = "Consejo"
content.body = "Haga clic en la notificacion para ver el consejo del dia."
content.sound = UNNotificationSound.default()
var dateComponents = DateComponents()
if let hour = pickerdatehour {
if let minute = pickerdateminute{
dateComponents.hour = hour
dateComponents.minute = minute
dateComponents.second = 0
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
center.add(request, withCompletionHandler: nil)
我试图从应用程序委托执行的代码是:
import UIKit
import Firebase
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("tes")
let sb = UIStoryboard(name: "Main", bundle: nil)
let otherVC = sb.instantiateViewController(withIdentifier: "consejos") as! ConsejosViewController
window?.rootViewController = otherVC;
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("tes")
let sb = UIStoryboard(name: "Main", bundle: nil)
let otherVC = sb.instantiateViewController(withIdentifier: "consejos") as! ConsejosViewController
window?.rootViewController = otherVC;
}
}
我做错了什么? app delegate 上的代码甚至从未执行过。
感谢任何帮助
您需要将 UNUserNotificationCenter
委托设置为自己来调用 UNUserNotificationCenter
的委托方法,如下所示:
let center = UNUserNotificationCenter.current()
center.delegate = self