Localnotification只显示两个Actions?

时间:2015-12-01 22:02:48

标签: ios swift localnotification

我想触发一个简单的本地通知并显示三个动作。我遵循了不同的教程(例如https://gist.github.com/kristopherjohnson/06bab43acbd9cebe5fd7https://github.com/ariok/TB_InteractiveNotifications/blob/master/TB_InteractiveNotifications/AppDelegate.swift),它们都有效(=触发通知)。

然而,无论我做什么,锁屏都只显示两个动作(教程都有三个动作)。我甚至评论了最小上下文(只显示了两个动作)。

我尝试了iphone 4s,5和6(s)模拟器,但它们都有相同的结果。

有人能指出我正确的方向吗?

ViewController.swift:

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

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let notification = UILocalNotification()
    notification.alertBody = "Hey! Update your counter ;)"
    notification.soundName = UILocalNotificationDefaultSoundName
    notification.fireDate = NSDate(timeIntervalSinceNow: 5)
    notification.category = categoryID

    UIApplication.sharedApplication().scheduleLocalNotification(notification)

}

Appdelegate.swift:

enum Actions:String{
    case increment = "INCREMENT_ACTION"
    case decrement = "DECREMENT_ACTION"
    case reset = "RESET_ACTION"
}

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

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

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

    // increment Action
    let incrementAction = UIMutableUserNotificationAction()
    incrementAction.identifier = Actions.increment.rawValue
    incrementAction.title = "ADD +1!"
    incrementAction.activationMode = UIUserNotificationActivationMode.Background
    incrementAction.authenticationRequired = true
    incrementAction.destructive = false

    // decrement Action
    let decrementAction = UIMutableUserNotificationAction()
    decrementAction.identifier = Actions.decrement.rawValue
    decrementAction.title = "SUB -1"
    decrementAction.activationMode = UIUserNotificationActivationMode.Background
    decrementAction.authenticationRequired = true
    decrementAction.destructive = false

    // reset Action
    let resetAction = UIMutableUserNotificationAction()
    resetAction.identifier = Actions.reset.rawValue
    resetAction.title = "RESET"
    resetAction.activationMode = UIUserNotificationActivationMode.Foreground
    // NOT USED resetAction.authenticationRequired = true
    resetAction.destructive = true


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

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

    // A. Set actions for the default context
    counterCategory.setActions([incrementAction, decrementAction, resetAction],
        forContext: UIUserNotificationActionContext.Default)




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

    let types = UIUserNotificationType.Alert
    let settings = UIUserNotificationSettings(forTypes: types, categories: NSSet(object: counterCategory) as? Set<UIUserNotificationCategory>)
    UIApplication.sharedApplication().registerUserNotificationSettings(settings)
    return true
}

来源:https://github.com/ariok/TB_InteractiveNotifications/blob/master/TB_InteractiveNotifications/AppDelegate.swift

0 个答案:

没有答案