如何在 Swift 4 中应用处于 运行 状态时显示推送通知?

How to show push notification while app is in running state in Swift 4?

虽然直到 Swift 3 我们使用 didReceiveRemoteNotification userInfo: [AnyHashable : Any] 方法它工作正常,但在 Swift 4 它不起作用。有人知道吗?

应用程序在前台时显示通知,使用以下方法。

// This method will be called when app received push notifications in foreground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) 
{
    completionHandler([.alert, .badge, .sound])
}

根据 apple documentation,您可以在应用处于 运行

时显示通知