Alexandre C. do Carmo
Alexandre C. do Carmo

Reputation: 701

UIUserNotificationType

I'm change my app to Swift 3, I finished almost everything, but I have a code on AppDelegate that I tried change to Swift 3 and dont worked. I read the documentation but dont worked How can I write the below code on Swift 3:

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


    let types:UIUserNotificationType = [UIUserNotificationType.alert, UIUserNotificationType.badge, UIUserNotificationType.sound]  

    let viewAction = UIMutableUserNotificationAction()  
    viewAction.identifier = "VIEW_IDENTIFIER"  
    viewAction.title = "title"  
    viewAction.activationMode = .foreground  

    let newsCategory = UIMutableUserNotificationCategory()  
    newsCategory.identifier = "test"  
    newsCategory.setActions([viewAction], for: .default)  

    application.registerUserNotificationSettings(UIUserNotificationSettings(types: types, categories: [newsCategory]))  
    application.registerForRemoteNotifications()  


      ...  

}  
func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, for notification: UILocalNotification, completionHandler: @escaping () -> Void) {

    if identifier == "openMyApp" {
        NotificationCenter.default.post(name: Notification.Name(rawValue: "openNotification"), object: nil)
    }

    completionHandler()
}

Upvotes: 0

Views: 623

Answers (1)

Pierce
Pierce

Reputation: 3158

With iOS 10.0 you need to use UNUserNotificationCenter, I think this is what you're looking for:

First import the UserNotifications framework, at the top of AppDelegate:

import UserNotifications

Then in didFinishLaunchingWithOptions:

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

    var options = UNAuthorizationOptions()
    options = [.badge, .alert, .sound]

    UNUserNotificationCenter.current().requestAuthorization(options: options) { (granted, error) in

        if granted {

            application.registerForRemoteNotifications()

        }

    }

    return true
}

Upvotes: 2

Related Questions