How to use UNNotificationPresentationOptions
Asked Answered
T

2

5

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

Territorialize answered 5/10, 2016 at 7:46 Comment(0)
T
12

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")
            }            
        }
Territorialize answered 5/10, 2016 at 8:45 Comment(1)
Don't forget to call completionHandler() at the end of userNotificationCenter(_:didReceive:withCompletionHandler:). If you don't call it, you'll probably get unwanted behavior. See the docs for more info.Gambeson
P
0

The new iOS 10 UNUserNotificationCenterDelegate now has a single set of methods for handling both remote and local notifications.

UNUserNotificationCenterDelegate protocol:

userNotificationCenter(_:didReceive:withCompletionHandler:)

Called to let your app know which action was selected by the user for a given notification.

userNotificationCenter(_:willPresent:withCompletionHandler:)

Delivers a notification to an app running in the foreground.

Two methods. And what’s better, is that now that they are moved into their own protocol, the iOS 10

UNUserNotificationCenterDelegate so this will help clean up your existing UIApplicationDelegate by being able to refactor all that old notification handling code into a shiny, new, cohesive protocol of it’s own.

Here’s an example:

extension NotificationManager: UNUserNotificationCenterDelegate {

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {

    switch response.actionIdentifier {

    // NotificationActions is a custom String enum I've defined
    case NotificationActions.HighFive.rawValue:
        print("High Five Delivered!")
    default: break
    }
}

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {

    // Delivers a notification to an app running in the foreground.
}
}
Pathe answered 5/10, 2016 at 8:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.