使用未声明的类型 'UNUserNotificationCenter'
Use of undeclared type 'UNUserNotificationCenter'
我想在应用程序处于前台时显示推送通知的横幅。我实现了这个方法来显示通知:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler([.alert, .badge, .sound])
}
但是收到此错误使用未声明的类型'UNUserNotificationCenter'
您所要做的就是导入 UserNotifications 框架:
import UserNotifications
此外,请确保您符合 UNUserNotificationCenterDelegate。
作为一种好的做法,我建议通过将其实现为 extension
:
如果您不熟悉 Delegation,您可能需要查看 。
import UIKit
// add this:
import UserNotifications
class ViewController: UIViewController {
.
.
.
// somewhere in your code:
UNUserNotificationCenter.current().delegate = delegateObject
}
// add this:
// MARK:- UserNotifications
extension ViewController: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler([.alert, .badge, .sound])
}
}
我想在应用程序处于前台时显示推送通知的横幅。我实现了这个方法来显示通知:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler([.alert, .badge, .sound])
}
但是收到此错误使用未声明的类型'UNUserNotificationCenter'
您所要做的就是导入 UserNotifications 框架:
import UserNotifications
此外,请确保您符合 UNUserNotificationCenterDelegate。
作为一种好的做法,我建议通过将其实现为 extension
:
如果您不熟悉 Delegation,您可能需要查看
import UIKit
// add this:
import UserNotifications
class ViewController: UIViewController {
.
.
.
// somewhere in your code:
UNUserNotificationCenter.current().delegate = delegateObject
}
// add this:
// MARK:- UserNotifications
extension ViewController: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler([.alert, .badge, .sound])
}
}