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

Posted

技术标签:

【中文标题】Swift 3 - 本地通知的 didReceiveRemoteNotification 函数未触发【英文标题】:Swift 3 - Local Notification's didReceiveRemoteNotification function not fired 【发布时间】:2016-12-29 15:39:56 【问题描述】:

我已设法安排通知,并在应用程序在前台运行/未运行时向用户显示。现在我需要在点击此通知时显示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 中与通知相关的全部代码。我错过了什么吗?

【问题讨论】:

【参考方案1】:

对于UserNotifications 框架,您需要使用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:)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 了解更多详情。

【讨论】:

哈哈 Nirav 感谢发布我过去几天一直在问的所有 iOS 问题的答案。对它很新,所以我充满了问题;)是的,您的答案有效,但是在收到通知时它会被触发。 我需要实现什么函数来捕获“tap”事件? @RickGrimesLikesWalkerSoup 为此实现此方法developer.apple.com/reference/usernotifications/… of UNUserNotificationCenterDelegate 美丽。有用。我可以问最后一个子问题,如果不是太麻烦。如何识别被点击的通知?如通过标识符识别通知,然后打开不同的视图控制器? @RickGrimesLikesWalkerSoup 您可以使用response.actionIdentifier 获得。 你能给我一个例子吗?

以上是关于Swift 3 - 本地通知的 didReceiveRemoteNotification 函数未触发的主要内容,如果未能解决你的问题,请参考以下文章

本地通知 swift 3

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

Swift 3 本地通知未触发

从 Swift 3 中的锁定屏幕清除本地通知

Swift 3 本地通知 - 设置自动时区

使用本地通知 api 在 swift 3 中触发每周通知