swift,来自视图控制器的UIUserNotificationAction而不是app delegate

时间:2016-08-21 19:51:15

标签: ios swift

我正在尝试从ViewController定义用户通知操作。根据我的理解,必须在App Delegate中设置Notification with Actions。

所以这是我的App代表:

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
}

这很有效。当我将“notification.category = categoryID”添加到我的一个LocalNotifications时,它会记录“完成”。现在我想调用的动作实际上应该从视图控制器调用。它有很多变量,您必须首先登录才能访问服务器以调用操作等。我该怎么做?当我将这个func放在我的视图控制器中时,操作不会被调用:

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()
}

非常感谢! 斯蒂芬

2 个答案:

答案 0 :(得分:0)

此函数是UIApplicationDelegate协议的一部分,该协议由您的AppDelegate实现,它是一种UIApplication实例。如果你有参考,你可以从这个函数中调用你的控制器。

答案 1 :(得分:0)

我让它与NSNotification合作。

NSNotificationCenter.defaultCenter().postNotificationName
在Appdelegate中的触发器是

。和

NSNotificationCenter.defaultCenter().addObserver

在我的视图控制器的viewdidload中。

最佳

斯蒂芬