StephanD
StephanD

Reputation: 141

swift, UIUserNotificationAction from view controller instead of app delegate

i´m trying to define an User Notification Action from a ViewController. From my understanding the Notification with Actions has to be set up in the App Delegate.

So here´s my App Delegate:

class AppDelegate: UIResponder, UIApplicationDelegate {




enum Actions:String{
    case confirmunlock = "CONFIRMUNLOCK_ACTION"
}

var categoryID:String {
    get{
        return "CONFRIMUNLOCK_CATEGORY"
    }
}


// Register notification settings
func registerNotification() {

    // 1. Create the actions **************************************************

    // confirmunlock Action
    let confirmunlockAction = UIMutableUserNotificationAction()
    confirmunlockAction.identifier = Actions.confirmunlock.rawValue
    confirmunlockAction.title = "Öffnen"
    confirmunlockAction.activationMode = UIUserNotificationActivationMode.Background
    confirmunlockAction.authenticationRequired = false
    confirmunlockAction.destructive = false




    // 2. Create the category ***********************************************

    // Category
    let confirmunlockCategory = UIMutableUserNotificationCategory()
    confirmunlockCategory.identifier = categoryID

    // A. Set actions for the default context
    confirmunlockCategory.setActions([confirmunlockAction],
                               forContext: UIUserNotificationActionContext.Default)

    // B. Set actions for the minimal context
    confirmunlockCategory.setActions([confirmunlockAction],
                               forContext: UIUserNotificationActionContext.Minimal)


    // 3. Notification Registration *****************************************

    let types: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Sound]
    let settings = UIUserNotificationSettings(forTypes: types, categories: NSSet(object: confirmunlockCategory) as? Set<UIUserNotificationCategory>)
    UIApplication.sharedApplication().registerUserNotificationSettings(settings)
}


// MARK: Application Delegate


func application(application: UIApplication,
                 handleActionWithIdentifier identifier: String?,
                                            forLocalNotification notification: UILocalNotification,
                                                                 completionHandler: () -> Void) {

    // Handle notification action *****************************************
    if notification.category == categoryID {

        let action:Actions = Actions(rawValue: identifier!)!

        switch action{

        case Actions.confirmunlock:
            print("Well done!")

        }
    }

    completionHandler()
}



var window: UIWindow?




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

    // Override point for customization after application launch.

    registerNotification()

    return true
}

This works well. When I add "notification.category = categoryID" to one of my LocalNotifications it Logs "Well done". Now the action i would like to call should actually be called from the view controller. It has a lot of variables and you have to be logged in first to access a server to call the action etc. How can I do that? When I put this func in my view controller the action is not getting called:

func application(application: UIApplication,
                 handleActionWithIdentifier identifier: String?,
                                            forLocalNotification notification: UILocalNotification,
                                                                 completionHandler: () -> Void) {

    // Handle notification action *****************************************
    if notification.category == categoryID {

        let action:Actions = Actions(rawValue: identifier!)!

        switch action{

        case Actions.confirmunlock:
            print("Well done!")

        }
    }

    completionHandler()
}

Many Thanks! Stephan

Upvotes: 0

Views: 262

Answers (2)

StephanD
StephanD

Reputation: 141

I got it to work with NSNotification.

NSNotificationCenter.defaultCenter().postNotificationName

in the Appdelegate where the trigger is. And

NSNotificationCenter.defaultCenter().addObserver

in the viewdidload of my view controller.

Best Stephan

Upvotes: 0

M_G
M_G

Reputation: 1208

This function is part of the UIApplicationDelegate protocol which is implemented by your AppDelegate which is an instance of kind UIApplication. You could call your controller from within this function if you have a reference.

Upvotes: 0

Related Questions