13

i want to show banner of push notification when app is in foreground. And i implementing this method to show notification:

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

but this error received Use of undeclared type 'UNUserNotificationCenter' enter image description here

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
Shahbaz Akram
  • 1,598
  • 3
  • 28
  • 45

1 Answers1

28

All you have to do is to import the UserNotifications framework:

import UserNotifications

Also, make sure that you conforming to UNUserNotificationCenterDelegate. As a good practice, I would suggest to do it by implementing it as an extension:

If you are unfamiliar with Delegation, you might want check this out.

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])
    }
}
Ahmad F
  • 30,560
  • 17
  • 97
  • 143