Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of undeclared type 'UNUserNotificationCenter'

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

like image 600
Shahbaz Akram Avatar asked Jul 21 '17 06:07

Shahbaz Akram


Video Answer


1 Answers

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])
    }
}
like image 166
Ahmad F Avatar answered Oct 22 '22 02:10

Ahmad F