gpt4 book ai didi

ios - 使用未声明的类型 'UNUserNotificationCenter'

转载 作者:IT王子 更新时间:2023-10-29 05:29:25 25 4
gpt4 key购买 nike

我想在应用程序处于前台时显示推送通知的横幅。我实现了这个方法来显示通知:

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

但收到此错误使用未声明的类型“UNUserNotificationCenter” enter image description here

最佳答案

您所要做的就是导入 UserNotifications框架:

import UserNotifications

此外,请确保您符合 UNUserNotificationCenterDelegate .作为一种好的做法,我建议通过将其实现为 extension 来做到这一点:

如果您不熟悉委派,您可能需要查看 this出。

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])
}
}

关于ios - 使用未声明的类型 'UNUserNotificationCenter',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45230784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com