Swift 3 - 本地通知的didReceiveRemoteNotification函数未被触发

时间:2016-12-29 15:39:56

标签: swift notifications swift3 uilocalnotification usernotifications

我已设法安排通知,并在应用正在运行/未在前台运行时向用户显示。现在我需要在点击此通知时显示[u"M&A simmers as producers swallow up brands to win shelf space, writes Neil Munhsi Pickles may go with sandwiches, as Hillshire Brands chief executive Sean Connolly put it two weeks ago. But many were puzzled by the US food group's announcement that it would pay $6.6bn to acquire New Jersey-based rival Pinnacle Foods, maker of Vlasic pickles and Birds Eye frozen food. Without the sort of mooted cost savings necessary to justify the purchase price, many saw the move by Hillshire, known in the US for Ball Park hot dogs and Jimmy Dean sausages, as a way to head off a potential takeover.\xa0 "]

我理解ViewController是用户点击通知时调用的函数。在我的情况下,永远不会触发此功能。

的AppDelegate:

didReceiveRemoteNotification

didReceiveRemoteNotification函数:

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

这是我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. } } 中与整个通知相关的代码。我错过了什么吗?

1 个答案:

答案 0 :(得分:2)

对于UserNotifications框架,您需要使用UNUserNotificationCenterDelegate,因此请使用UNUserNotificationCenterDelegate实施AppDelegate并在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:)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了解详情。

相关问题