我正在关注本教程:https://medium.com/flawless-app-stories/ios-remote-push-notifications-in-a-nutshell-d05f5ccac252

但是由于某种原因,我越来越


  无法将“ AppDelegate”分配给“ UNUserNotificationCenterDelegate”。


那条线是做什么的?如果我将其注释掉,其余代码将正常工作,并且如果用户希望允许通知,则会提示用户。

func registerForPushNotifications() {
    UNUserNotificationCenter.current().delegate = self // line in question
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
        (granted, error) in
        print("Permission granted: \(granted)")
        // 1. Check if permission granted
        guard granted else { return }
        // 2. Attempt registration for remote notifications on the main thread
        DispatchQueue.main.async {
            UIApplication.shared.registerForRemoteNotifications()
        }
    }
}

最佳答案

您首先需要在import类中delegate相关的(UNUserNotificationCenterDelegate) AppDelegate,如下面的代码所示。

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate


在您的代码UNUserNotificationCenter.current().delegate = self中,self无法响应所需的delegate,这就是您收到错误消息的原因。

10-08 05:31