【发布时间】:2025-11-28 14:50:01
【问题描述】:
我已设法安排通知,并在应用程序在前台运行/未运行时向用户显示。现在我需要在点击此通知时显示ViewController。
我了解didReceiveRemoteNotification 是用户点击通知时调用的函数。在我的例子中,这个函数永远不会被触发。
AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
return true
}
//didReceiveRemoteNotification goes here
didReceiveRemoteNotification 函数:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
if ( application.applicationState == UIApplicationState.active)
{
print("Active")
// App is foreground and notification is recieved,
// Show a alert.
}
else if( application.applicationState == UIApplicationState.background)
{
print("Background")
// App is in background and notification is received,
// You can fetch required data here don't do anything with UI.
}
else if( application.applicationState == UIApplicationState.inactive)
{
print("Inactive")
// App came in foreground by used clicking on notification,
// Use userinfo for redirecting to specific view controller.
}
}
这是我AppDelegate 中与通知相关的全部代码。我错过了什么吗?
【问题讨论】:
标签: swift notifications swift3 uilocalnotification usernotifications