IOS 8 Local Notification with Actions

Quick note on how to set up local notification with actions in IOS 8:

1. Set up UIMutableUserNotificationAction.  You need to the following properties:

  • identifier (a string that will be used for respond to the action that user requested)
  • title (string to display on the button)
  • activationMode (enum for UIUserNotificationActivationMode: Foreground or Background.  define if the action running in background or bring the app to the foreground)
  • destructive (boolean, if set true the button will be red color)
  • authenticationRequired (boolean, set to true if you are doing some harmful action so that no other people can respond to the action)

2. Set up UIMutableUserNotificationCategory and set the identifier property.   We will group actions into different categories, and we will just use the category identifier to identify which set of actions will be used.

3. set category actions.  define default and minimal actions for different context.

4. Add category and type of notifications that will be used to UIUserNotificationSettings.

5. Register settings.  UIApplication.sharedApplication().registerUserNotifcationSettings(settings) and we are ready to send notification.

6. When creating a UILocalNotification, we will set the following properties:

  • category (category identifier we defined earlier.  actions belong to that category will be added to the notification)
  • alertBody (string, message to be displayed)
  • soundName (optional. sound for the notification)
  • fireDate (if you would like to schedule the notification to be display later, set this property)

7. Now you can call UIApplication.shareApplication().presentLocalNotificationNow(notification) or UIApplication.shareApplication().scheduleLocalNotification(notification)

// Set up Actions for notifications
let viewDetailAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
viewDetailAction.identifier = "VIEW_ACTION"
viewDetailAction.title = "View Detail"
viewDetailAction.activationMode = UIUserNotificationActivationMode.Foreground
viewDetailAction.destructive = false
viewDetailAction.authenticationRequired = true
let cancelAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
cancelAction.identifier = "CANCEL_ACTION"
cancelAction.title = "Cancel"
cancelAction.activationMode = UIUserNotificationActivationMode.Background
cancelAction.destructive = true
cancelAction.authenticationRequired = false
// set up category
let category = UIMutableUserNotificationCategory()
category.identifier = "FIRST_CATEGORY" // id to be to reference later
// add actions to the array
let defaultAction:NSArray = [viewDetailAction, cancelAction]
category.setActions(defaultAction, forContext: UIUserNotificationActionContext.Default)
// For minimal (if there are more than two actions, use UIUserNotificationActionContext.Minimal)
// put category in to NSSet; we only have one category now
let categories = NSSet(object: category)
// set type of notification will be using
let types = UIUserNotificationType.Badge | UIUserNotificationType.Alert | UIUserNotificationType.Sound
// create UIUserNotificationSettings
let settings = UIUserNotificationSettings(forTypes: types, categories: categories)
// Finally, register setting
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
// Now create UILocalNotification
let notification = UILocalNotification()
notification.category = "FIRST_CATEGORY" // we will using this category that we defined earlier
notification.alertBody = "This is first Notice!"
notification.soundName = UILocalNotificationDefaultSoundName // we will use default sound
// if you want to schedule, set the time in notification.fireDate and scheduleLocalNotification()
UIApplication.sharedApplication().presentLocalNotificationNow(notification) // this will show notification now

Read More