Local notifications with quick actions

This tutorial has been updated for X code 7.3 & swift 2.2

In iOS 8 Apple launched interactive local notifications. Instead of just showing the text with local notification now you can take some quick actions after reading the text. If you just want to create simple notification without action then check out my schedule & reschedule local notification tutorial here. If you want to learn local notification with quick actions then keep reading.

  1. First lets set up the x code project as usual. Create new single view application X code project. Set project name to notification actions & save it on desktop.

new x code project

2. Go to main storyboard and select view controller. Set its size iPhone 4-inch in attribute section.

set size to iPhone 4 inch     3. Add date picker at bottom of view controller, add textfield with width 200 and add button as follows.

add text field, button & date picker

4. Connect text field as outlet to view controller swift file

connect textfield as outlet

5. Connect date picker as outlet to view controller swift file

connect date picker as outlet

6. Before we save local notification lets create notification actions and ask for permission to show notifications. Go to app delegate swift file and add the following code in did finish launching with options method

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
// 1
let doNothingAction = UIMutableUserNotificationAction()
// 2
doNothingAction.identifier = “justInform”
// 3
doNothingAction.title = “Thanks, Got it”
// 4
doNothingAction.activationMode = UIUserNotificationActivationMode.Background
// 5
doNothingAction.destructive = false
// 6
doNothingAction.authenticationRequired = false
return true

}
  1. Create first notification action
  2. Set its identifier to any string value. This is a identifier just as table view cell identifier or segue identifier. So it should be unique. Do not set same identifier to two different notification actions. And identifier is single continuous word. There is no space in identifier string.
  3. Set notification actions button title.
  4. After you press the button; do you want to run action in background mode or foreground mode. App will open in foreground mode & app will not open in background mode.
  5. If this action executes non reversible action then you need to display that in destructive mode.
  6. For this action to execute does the user have to unlock the phone or can you execute action without unlocking the phone.

7. Now create 3 more notification actions as follows

	func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
// 1
let doNothingAction = UIMutableUserNotificationAction()
// 2
doNothingAction.identifier = “justInform”
// 3
doNothingAction.title = “Thanks, Got it”
// 4
doNothingAction.activationMode = UIUserNotificationActivationMode.Background
// 5
doNothingAction.destructive = false
// 6
doNothingAction.authenticationRequired = false

// 7
let remindAction = UIMutableUserNotificationAction()

remindAction.identifier = "remindAgain"

remindAction.title = "Remind in 30 min"

remindAction.activationMode = UIUserNotificationActivationMode.Background

remindAction.destructive = false

remindAction.authenticationRequired = false


let completedAction = UIMutableUserNotificationAction()

completedAction.identifier = "completed"

completedAction.title = "Completed task"

completedAction.activationMode = UIUserNotificationActivationMode.Background

completedAction.destructive = false

completedAction.authenticationRequired = true


let deleteAction = UIMutableUserNotificationAction()

deleteAction.identifier = "delete"

deleteAction.title = "Delete reminder"

deleteAction.activationMode = UIUserNotificationActivationMode.Background

deleteAction.destructive = true

deleteAction.authenticationRequired = true

return true

}

8. Just below delete action create categories for actions. This is not optional. If you want to use actions then you have to create categories

deleteAction.authenticationRequired = true

// 1
let reminderCategory = UIMutableUserNotificationCategory()
// 2
reminderCategory.identifier = "quickAction"
// 3
reminderCategory.setActions([doNothingAction, remindAction, completedAction, deleteAction], forContext: UIUserNotificationActionContext.Default)
// 4
reminderCategory.setActions([remindAction, deleteAction], forContext: UIUserNotificationActionContext.Minimal)
  1. Create notification category.
  2. Set its identifier to any string value. Same rules apply here as notification action identifier.
  3. When notification action context is default then you want to show all the notification actions. You can set maximum of 4 notification actions. They will be displayed in that order. If you set 5th action notification then it will not be displayed.
  4. When notification action context is minimal then you can show maximum of two actions only. These will be displayed on iOS notifications when you pull the notification immediately on receiving. Or if you slide old notification to left then you can see two actions also.

9. Just below categories register these actions & categories in local notification. Then register the local notification as follows.

reminderCategory.setActions([remindAction, deleteAction], forContext: UIUserNotificationActionContext.Minimal)
// 1
let categories = Set<UIUserNotificationCategory>(arrayLiteral: reminderCategory)
// 2
let notificationSettings = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound], categories: categories)
// 3
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
// 4
application.applicationIconBadgeNumber = 0

return true

}
  1. Create a set of categories from single category. Here you can save multiple categories if you want.
  2. Create notification settings with all types (alert, badge & sound notification type) & save set of categories.
  3. Register these notification settings with application.
  4. When you open app; reset badge number to zero.

10. The whole code looks like follows

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
// 1
let doNothingAction = UIMutableUserNotificationAction()
// 2
doNothingAction.identifier = “justInform”
// 3
doNothingAction.title = “Thanks, Got it”
// 4
doNothingAction.activationMode = UIUserNotificationActivationMode.Background
// 5
doNothingAction.destructive = false
// 6
doNothingAction.authenticationRequired = false

// 7
let remindAction = UIMutableUserNotificationAction()

remindAction.identifier = "remindAgain"

remindAction.title = "Remind in 30 min"

remindAction.activationMode = UIUserNotificationActivationMode.Background

remindAction.destructive = false

remindAction.authenticationRequired = false


let completedAction = UIMutableUserNotificationAction()

completedAction.identifier = "completed"

completedAction.title = "Completed task"

completedAction.activationMode = UIUserNotificationActivationMode.Background

completedAction.destructive = false

completedAction.authenticationRequired = true


let deleteAction = UIMutableUserNotificationAction()

deleteAction.identifier = "delete"

deleteAction.title = "Delete reminder"

deleteAction.activationMode = UIUserNotificationActivationMode.Background

deleteAction.destructive = true

deleteAction.authenticationRequired = true

// 1
let reminderCategory = UIMutableUserNotificationCategory()
// 2
reminderCategory.identifier = "quickAction"
// 3
reminderCategory.setActions([doNothingAction, remindAction, completedAction, deleteAction], forContext: UIUserNotificationActionContext.Default)
// 4
reminderCategory.setActions([remindAction, deleteAction], forContext: UIUserNotificationActionContext.Minimal)
// 1
let categories = Set<UIUserNotificationCategory>(arrayLiteral: reminderCategory)
// 2
let notificationSettings = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound], categories: categories)
// 3
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
// 4
application.applicationIconBadgeNumber = 0

return true

}

11. Connect button as action to view controller swift file

connect button as action

12. Fix the mistake in second in date picker with following method

 // 12
 func fixedNotificationDate(dateToFix: NSDate) -> NSDate {
let dateComponents: NSDateComponents = NSCalendar.currentCalendar().components([NSCalendarUnit.Day, NSCalendarUnit.Month, NSCalendarUnit.Year, NSCalendarUnit.Hour, NSCalendarUnit.Minute], fromDate: dateToFix)

dateComponents.second = 0

let fixedDate: NSDate = NSCalendar.currentCalendar().dateFromComponents(dateComponents)!

return fixedDate

}

13. Create local notification in set reminder pressed method as follows

@IBAction func setReminderPressed(sender: UIButton) {

let notification = UILocalNotification()

notification.fireDate = fixedNotificationDate(datePicker.date)

notification.timeZone = NSTimeZone.defaultTimeZone()

notification.applicationIconBadgeNumber = 1

if textField.text == "" {

    notification.alertBody = "Start cooking dinner"

}
else {

    notification.alertBody = textField.text

}

// 1
notification.alertAction = "View reminder"
// 2
notification.category = "quickAction"
// 3
notification.soundName = UILocalNotificationDefaultSoundName

UIApplication.sharedApplication().scheduleLocalNotification(notification)

}

I am only going to show the differences from schedule or reschedule local notification tutorial

  1. This part is optional. If you don’t set this action then still a button would appear at first position. It will be named as “Open” & it just opens app if pressed. I am renaming the button to “View reminder”.
  2. This is where you set notification category. If you have multiple categories then each should have their own unique identifier. That you can set here.
  3. Because I am using sound notification; I am also setting which tune should be used for sound notification. I am simply using default tune.

14. Run the application in iPhone 5s & set reminders. Default notification setting is banner as follows.

banner notification setting

Screen Shot 2015-10-19 at 10.37.05 PM

Screen Shot 2015-10-19 at 10.41.13 PM

If you set the notification settings to alerts then you get following notification

Screen Shot 2015-10-19 at 10.38.52 PM

Screen Shot 2015-10-19 at 10.40.04 PM

Screen Shot 2015-10-19 at 10.40.21 PM
Download X code file
Challenge : Make changes in app after you press notification actions.
Download Challenge Solution

Total 1 Votes
0

Tell us how can I improve this tutorial?

+ = Verify Human or Spambot ?