Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use UNNotificationPresentationOptions

In iOS 10 , there is an option for presenting the notification when the app is in foreground using UNNotificationPresentationOptions,

but i couldn't find any sample on how to use this, please suggest some idea about how to implement this feature

like image 866
satheesh Avatar asked Dec 24 '22 00:12

satheesh


1 Answers

I have implemented the foreground notification by

Adding the below code in my viewController

extension UIViewController: UNUserNotificationCenterDelegate {

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

    public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Swift.Void) {
        print("Do what ever you want")

    }

}

In my Appdelegate on didFinishLaunchingWithOptions

UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound]) {(accepted, error) in

            if !accepted {   
                print("Notification access denied")
            }            
        }
like image 94
satheesh Avatar answered Jan 02 '23 16:01

satheesh