Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using UNUserNotificationCenter for iOS 10

Attempting to work with Firebase to register for remote notifications however when implementing the following code I get the error:

UNUserNotificationCenter is only available on iOS 10.0 or newer

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        var soundID: SystemSoundID = 0
        let soundFile: String = NSBundle.mainBundle().pathForResource("symphony", ofType: "wav")!
        let soundURL: NSURL = NSURL(fileURLWithPath: soundFile)
        AudioServicesCreateSystemSoundID(soundURL, &soundID)
        AudioServicesPlayAlertSound(soundID)
        Fabric.with([Twitter.self])


        //Firebase configuration
        FIRApp.configure()

        //Resource code from stackoverflow to create UNUserNotificationCenter
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
            // Enable or disable features based on authorization.
        }
        application.registerForRemoteNotifications()
        return true
    }

By doing a simple "Fix-it" doesn't resolve my issue by creating an if statement based on the OS version number. What should I be doing or thinking towards this solution for the UserNotifications framework?

like image 387
Laurence Wingo Avatar asked Jan 05 '23 15:01

Laurence Wingo


1 Answers

For one thing, with the new UNUserNotificationCenter, you only want to register for remote notifications if the user grants permission. The way your code is setup, you're trying to do it regardless of permission which could be one of the reasons. You should do something like this:

import UserNotifications

...

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

    UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in

        DispatchQueue.main.async {
            UIApplication.shared.registerForRemoteNotifications()
        }

    }

    return true
}

If you need to check if the user has an OS lower than iOS 10.0 - you could try something like this to include the old system:

if #available(iOS 10.0, *) {

    UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in

        DispatchQueue.main.async {
            UIApplication.shared.registerForRemoteNotifications()
        }

    }

} else {

    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert |
        UIUserNotificationType.Badge, categories: nil))
}

Let me know if this works, and if it's what you're trying to accomplish. If not, I'll remove my answer.

like image 144
Pierce Avatar answered Jan 14 '23 12:01

Pierce