0

我设法安排了一个通知,并在应用运行/未在前台运行时将其显示给用户。现在我需要在点击此通知后显示ViewControllerSwift 3 - 本地通知的didReceiveRemoteNotification函数未被触发

据我所知,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整个通知相关的代码。我错过了什么吗?

回答

2

对于UserNotifications框架,您需要使用UNUserNotificationCenterDelegate工作,所以实现UNUserNotificationCenterDelegateAppDelegate和设置委托在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了解更多详情。

+0

Haha Nirav感谢您在过去的几天中询问过的所有iOS问题的答案。非常新,所以我充满了问题;)是的,你的答案有效,但当收到通知时它会被解雇。 **我需要实现什么功能来捕获'tap'事件?** – Dinuka

+0

@RickGrimesLikesWalkerSoup对于'UNUserNotificationCenterDelegate'实现此方法https://developer.apple.com/reference/usernotifications/unusernotificationcenterdelegate/1649501-usernotificationcenter 。 –

+0

美丽。有用。请问最后一个问题是不是太麻烦。我怎样才能确定哪个通知被点击了?如在,通过它们的标识符来识别通知,然后打开不同的视图控制器? – Dinuka