Using the undeclared type "UNUserNotificationCenter"

I want to show the banner of push notifications when the application is in the foreground. And I implement this method for notification:

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

but this error is received Using Uneclared type 'UNUserNotificationCenter' enter image description here

+4
source share
1 answer

All you have to do is import the UserNotifications framework:

import UserNotifications

Also, make sure you match UNUserNotificationCenterDelegate . As a good practice, I would suggest doing this by doing it as extension:

, .

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

Source: https://habr.com/ru/post/1681939/


All Articles