To implement Notification in iOS 10, follow below steps.
Go to Project -> Target -> capabilities -> push notifications
enable push notification.
Now in Your AppDelegate Class do following steps -
1. Import UserNotifications.
2. Assign “UNUserNotificationCenterDelegate“ Delegate to this class.
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate
3. In didFinishLaunchingWithOptions method implement “UNUserNotificationCenter” delegate and “UIUserNotificationSettings”.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
// Enable or disable features based on authorization.
if granted == true
{
UIApplication.shared.registerForRemoteNotifications()
}}
return true
}
4.Now implement delegate methods of “UNUserNotificationCenter”
// The method will be called when application is in the foreground.
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
//You can set notification presented as a sound, badge, alert and/or in the notification list.
completionHandler(UNNotificationPresentationOptions.alert)
}
// The method will be called when the user tap on the notification by opening the application, dismissing the notification or choosing a UNNotificationAction.
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Swift.Void){
// Handle response accordingly.
}
0 Comment(s)