Try this
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge , .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
First: Import UserNotification
import UserNotifications
Second: add a delegate to appDelegate:
class AppDelegate: UIResponder, **UIApplicationDelegate**
Third: then register notifications:
if #available(iOS 10.0, *)
{
let center = UNUserNotificationCenter.currentNotificationCenter()
center.delegate = self
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
}
}
else
{
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
}
UIApplication.shared.registerForRemoteNotifications()
Swift 4.0
, , :
DispatchQueue.main.async(execute: {
UIApplication.shared.registerForRemoteNotifications()
})