0

I want to fire a simple local notification and display three actions. I followed different tutorials (e.g. https://gist.github.com/kristopherjohnson/06bab43acbd9cebe5fd7 or https://github.com/ariok/TB_InteractiveNotifications/blob/master/TB_InteractiveNotifications/AppDelegate.swift) and they all work (= fire the notification).

However, the lock screen only displays two actions no matter what I do (the tutorials all have three actions). I even commented out the minimal context (where only two actions are displayed).

I tried the iphone 4s, 5 and 6(s) simulator but they all have the same result.

Can anyone point me in the right direction?

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
}

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

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Saerdn
  • 218
  • 2
  • 17

0 Answers0