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'
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])
}
}