如何使用UNNotificationPresentationOptions

sat*_*esh 1 ios swift usernotifications

在iOS 10中,有一个选项可以在应用程序处于前台时使用UNNotificationPresentationOptions显示通知,

但是我找不到任何关于如何使用它的示例,请提出一些关于如何实现此功能的建议

sat*_*esh 7

我已经实现了前台通知

在我的viewController中添加以下代码

extension UIViewController: UNUserNotificationCenterDelegate {

    public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) {
        completionHandler( [.alert, .badge, .sound])
    }

    public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Swift.Void) {
        print("Do what ever you want")

    }

}
Run Code Online (Sandbox Code Playgroud)

在我的Appdelegate上的didFinishLaunchingWithOptions

UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound]) {(accepted, error) in

            if !accepted {   
                print("Notification access denied")
            }            
        }
Run Code Online (Sandbox Code Playgroud)