Swift 3 - 未触发本地通知的 didReceiveRemoteNotification 函数
Swift 3 - Local Notification's didReceiveRemoteNotification function not fired
我已经设法安排了一个通知,并在应用程序 running/not 运行 在前台时向用户显示。现在我需要在点击此通知时显示 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
中与通知相关的全部代码。我错过了什么吗?
对于 UserNotifications
framework you need to work with UNUserNotificationCenterDelegate
,所以用 AppDelegate
实现 UNUserNotificationCenterDelegate
并在 didFinishLaunchingWithOptions
方法中设置委托。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
return true
}
现在您需要实施 userNotificationCenter(_:willPresent:withCompletionHandler:)
and userNotificationCenter(_:didReceive:withCompletionHandler:)
方法来获取通知。
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
//Handle notification
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// pull out the buried userInfo dictionary
let userInfo = response.notification.request.content.userInfo
if let customData = userInfo["customData"] as? String {
print("Custom data received: \(customData)")
switch response.actionIdentifier {
case UNNotificationDefaultActionIdentifier:
// the user swiped to unlock
print("Default identifier")
case "show":
// the user tapped our "show more info…" button
print("Show more information…")
break
default:
break
}
}
// you must call the completion handler when you're done
completionHandler()
}
您还可以查看此 AppCoda 教程 Introduction to User Notifications Framework in iOS 10
了解更多详细信息。
我已经设法安排了一个通知,并在应用程序 running/not 运行 在前台时向用户显示。现在我需要在点击此通知时显示 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
中与通知相关的全部代码。我错过了什么吗?
对于 UserNotifications
framework you need to work with UNUserNotificationCenterDelegate
,所以用 AppDelegate
实现 UNUserNotificationCenterDelegate
并在 didFinishLaunchingWithOptions
方法中设置委托。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
return true
}
现在您需要实施 userNotificationCenter(_:willPresent:withCompletionHandler:)
and userNotificationCenter(_:didReceive:withCompletionHandler:)
方法来获取通知。
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
//Handle notification
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// pull out the buried userInfo dictionary
let userInfo = response.notification.request.content.userInfo
if let customData = userInfo["customData"] as? String {
print("Custom data received: \(customData)")
switch response.actionIdentifier {
case UNNotificationDefaultActionIdentifier:
// the user swiped to unlock
print("Default identifier")
case "show":
// the user tapped our "show more info…" button
print("Show more information…")
break
default:
break
}
}
// you must call the completion handler when you're done
completionHandler()
}
您还可以查看此 AppCoda 教程 Introduction to User Notifications Framework in iOS 10
了解更多详细信息。