Try this
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge , .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
First: import UserNotification
import UserNotifications
Second: Then add delegate to appDelegate :
class AppDelegate: UIResponder, **UIApplicationDelegate**
Third: Then Register notifications :
if #available(iOS 10.0, *)
{
let center = UNUserNotificationCenter.currentNotificationCenter()
center.delegate = self
//center.setNotificationCategories(nil)
center.requestAuthorizationWithOptions([.Alert,.Badge,.Sound]) {
granted,error in
if (error == nil) {
UIApplication.sharedApplication().registerForRemoteNotifications()
}
}
}
Swift 3.0
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
}
else
{
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
}
UIApplication.shared.registerForRemoteNotifications()
Swift 4.0
Do above steps then register notification on the main thread like:
DispatchQueue.main.async(execute: {
UIApplication.shared.registerForRemoteNotifications()
})