Swift 4 This works for me:
Step 1 into TARGETS Click on add capability and select Push Notifications
Step 2 in AppDelegate.swift add the following code:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound]) { (didAllow, error) in
}
UIApplication.shared.registerForRemoteNotifications()
return true
}
//Get device token
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
{
let tokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
print("The token: \(tokenString)")
}