代码之家  ›  专栏  ›  技术社区  ›  Shahbaz Akram

使用未声明的类型“UNUserNotificationCenter”

  •  12
  • Shahbaz Akram  · 技术社区  · 7 年前

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

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

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

    1 回复  |  直到 7 年前
        1
  •  28
  •   Ahmad F    7 年前

    您只需导入 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])
        }
    }