Asher Hawthorne Asher Hawthorne - 4 months ago 245x
iOS Question

Registering for Push Notifications in Xcode 8/Swift 3.0?

I'm trying to get my app working in Xcode 8.0, and am running into an error. I know this code worked fine in previous versions of swift, but I'm assuming the code for this is changed in the new version. Here's the code I'm trying to run:

let settings = UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil)

The error that I'm getting is "Argument labels '(forTypes:, categories:)' do not match any available overloads"

Is there a different command that I could try to get this working?


Request user permission

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        let center = UNUserNotificationCenter.current()
        center.requestAuthorization([.alert, .sound]) { (granted, error) in
            // Enable or disable features based on authorization.
        return true

Getting device token

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

    let characterSet :CharacterSet = CharacterSet(charactersIn: "<>")
    let deviceTokenString: String = (deviceToken.description)
        .trimmingCharacters(in: characterSet)
        .replacingOccurrences(of: " ", with: "")


In case of error

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {

    print("i am not available in simulator \(error)")


In case if you need to know the permissions granted

UNUserNotificationCenter.current().getNotificationSettings(){ (setttings) in

            switch setttings.soundSetting{
            case .enabled:

                print("enabled sound setting")

            case .disabled:

                print("setting has been disabled")

            case .notSupported:
                print("something vital went wrong here")