Testing Interactive Notifications in iOS .

Posted in: Blog

Before the release of iOS 8, developers didn’t have the power to provide users with any sort of performable actions in the notification bar. But now thanks to the interactive notifications in iOS 8, users can reply to messages right from the banner, accept invitations from the lock screen or ‘like’ an activity from the notification center. It allows them to perform significant actions in real time

Our Appdev360 engineers decided to experiment with this function, in a sample application, to thoroughly understand it’s working. They implemented the following delegate in the AppDelegate class.

func application(application: UIApplication, handleActionWithIdentifier identifier: String?,
forLocalNotification notification: UILocalNotification, completionHandler: () -> Void)
.

This delegate method is where the action input is handled using an identifier, such as:

if identifier == "actionOne" {
            NSNotificationCenter.defaultCenter().postNotificationName("actionOneNotification", object: nil)
        }
        else if identifier == "actionTwo" {
            NSNotificationCenter.defaultCenter().postNotificationName("actionTwoNotification", object: nil)

Appdev360 engineers added the following observers to controller’s viewDidLoad method for appropriately handling post notifications

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.actionOneNotification), name: "actionOneNotification", object: nil)

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.actionTwoNotification), name: "actionTwoNotification", object: nil)
.

Later, they set up notification settings by creating the action using UIMutableUserNotificationAction. A category was also created to set these actions using UIMutableUserNotificationCategory. After which, they successfully registered and scheduled the notification settings for the app.

 Scheduled the Notification

Testing-Interactive-Notifications-in-iOS-1

Notification Arrived

Testing-Interactive-Notifications-in-iOS-2

Swiped the notification to expose Actions

Testing-Interactive-Notifications-in-iOS-3

Action Tapped

Testing-Interactive-Notifications-in-iOS-4

Leave a Reply

Your email address will not be published. Required fields are marked *