{"id":45896,"date":"2017-02-10T13:26:50","date_gmt":"2017-02-10T07:56:50","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=45896"},"modified":"2017-02-10T13:26:50","modified_gmt":"2017-02-10T07:56:50","slug":"how-to-implement-interactive-notification-in-your-ios-application","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-implement-interactive-notification-in-your-ios-application\/","title":{"rendered":"How to Implement Interactive Notification in your iOS Application?"},"content":{"rendered":"<p><span style=\"color: #333333\">Receiving and managing notifications on <a title=\"iOS application development\" href=\"http:\/\/www.tothenew.com\/mobile-ios-application-development-services\">iOS<\/a>\u00a0is a nightmare until the introduction of Notification Centre in iOS 5. Android long held an advantage over iOS in terms of how easily users could interact. However,\u00a0Apple has evolved how its premiere OS presents notification from third-party <\/span>apps,<span style=\"color: #333333\"> but has limited developers to title and string of text.<\/span><\/p>\n<p><span style=\"color: #333333\">Interactive Notification in iOS 8 represented the first true enhancement to the functionality of notifications on the OS, allowing third\u2013party developers to surface a handful of options for user action following a swipe on a new alert. \u00a0For example &#8211; Received a new chat message? Swipe to initiate a reply; Got an alert for a new comment on Facebook. Tap the \u201cLike\u201d button without leaving the lock screen. This simple functionality coupled with smarter Notification Centre help users to interact with their application in a better way.<\/span><\/p>\n<p><span style=\"color: #333333\">Using interactive notifications, customers will have multiple options to respond using simple button taps, such as:<\/span><\/p>\n<ol>\n<li><span style=\"color: #333333\">Share<\/span><\/li>\n<li><span style=\"color: #333333\">Shop Now<\/span><\/li>\n<li><span style=\"color: #333333\">Download<\/span><\/li>\n<li><span style=\"color: #333333\">Check In<\/span><\/li>\n<li><span style=\"color: #333333\">Accept<\/span><\/li>\n<\/ol>\n<p><span style=\"color: #333333\">Or, if the customer is not interested, the following options can be given:<\/span><\/p>\n<ol>\n<li><span style=\"color: #333333\">Dismiss<\/span><\/li>\n<li><span style=\"color: #333333\">No Thanks<\/span><\/li>\n<li><span style=\"color: #333333\">Decline<\/span><\/li>\n<\/ol>\n<p align=\"LEFT\"><span style=\"color: #333333\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-45811\" src=\"\/blog\/wp-ttn-blog\/uploads\/2017\/02\/InteractiveNotify-300x182.png\" alt=\"InteractiveNotify\" width=\"300\" height=\"182\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2017\/02\/InteractiveNotify-300x182.png 300w, \/blog\/wp-ttn-blog\/uploads\/2017\/02\/InteractiveNotify-624x379.png 624w, \/blog\/wp-ttn-blog\/uploads\/2017\/02\/InteractiveNotify.png 720w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/span><\/p>\n<p><span style=\"color: #333333\"><strong>How to Implement Interactive Notification in your Application<\/strong><\/span><\/p>\n<p><span style=\"color: #333333\">Let&#8217;s\u00a0assume, we have a shopping application and a user who wants to shop certain products and have created a list of things he\/she wants to shop\u00a0at a particular time of the day. The user can schedule a reminder within the application with the list<\/span><\/p>\n<p><span style=\"color: #333333\">First thing in the application is that we are using local notifications instead of receiving\u00a0push notifications. So in this case, we need to register our local push notifications.<\/span><\/p>\n<p><span style=\"color: #333333\">So let&#8217;s get started.<\/span><\/p>\n<p><span style=\"color: #333333\"><strong>STEP 1 &#8211; \u00a0<\/strong><strong>Place below code in your AppDelegate.swift.<\/strong><\/span><\/p>\n<p><span style=\"color: #333333\">func\u00a0application(_\u00a0application:\u00a0UIApplication, didReceive notification:\u00a0UILocalNotification) {<\/span><br \/>\n<span style=\"color: #333333\"> \/\/ Do something serious in a real app.<\/span><br \/>\n<span style=\"color: #333333\"> print(&#8220;Received Local Notification:&#8221;)<\/span><br \/>\n<span style=\"color: #333333\"> print(notification.alertBody)<\/span><br \/>\n<span style=\"color: #333333\"> }<\/span><br \/>\n<span style=\"color: #333333\"> func\u00a0application(_\u00a0application:\u00a0UIApplication, handleActionWithIdentifier identifier:\u00a0String?,\u00a0for\u00a0notification:\u00a0UILocalNotification, completionHandler: @escaping () -&gt;\u00a0Void) {<\/span><br \/>\n<span style=\"color: #333333\"> if\u00a0identifier ==\u00a0&#8220;editList&#8221;\u00a0{<\/span><br \/>\n<span style=\"color: #333333\"> NotificationCenter.default.post(name: Notification.Name(rawValue:\u00a0&#8220;modifyListNotification&#8221;), object:\u00a0nil)<\/span><br \/>\n<span style=\"color: #333333\"> }<\/span><br \/>\n<span style=\"color: #333333\"> else\u00a0if\u00a0identifier ==\u00a0&#8220;trashAction&#8221;\u00a0{<\/span><br \/>\n<span style=\"color: #333333\"> NotificationCenter.default.post(name: Notification.Name(rawValue:\u00a0&#8220;deleteListNotification&#8221;), object:\u00a0nil)<\/span><br \/>\n<span style=\"color: #333333\"> }<\/span><br \/>\n<span style=\"color: #333333\"> completionHandler()<\/span><br \/>\n<span style=\"color: #333333\"> }<\/span><\/p>\n<p><span style=\"color: #333333\"><strong>STEP 2 &#8211; \u00a0Add\u00a0observers for these two identifiers for \u201cedit list\u201d and \u201ctrash Action\u201d.<\/strong><\/span><\/p>\n<p><span style=\"color: #333333\">Add the code given below in the viewDidLoad of view controller:<\/span><\/p>\n<p><span style=\"color: #333333\">NotificationCenter.default.addObserver(self, selector:\u00a0#selector(ViewController.handleModifyListNotification), name: NSNotification.Name(rawValue:\u00a0&#8220;modifyListNotification&#8221;), object:\u00a0nil)<\/span><br \/>\n<span style=\"color: #333333\"> NotificationCenter.default.addObserver(self, selector:\u00a0#selector(ViewController.handleDeleteListNotification), name: NSNotification.Name(rawValue:\u00a0&#8220;deleteListNotification&#8221;), object:\u00a0nil)<\/span><\/p>\n<p><span style=\"color: #333333\">Setup your code for local notification. Now the key thing would be Interactive Notification.\u00a0Now, create a method named setupNotificationSettings like below:<\/span><\/p>\n<p><span style=\"color: #333333\">func\u00a0setupNotificationSettings() {<\/span><br \/>\n<span style=\"color: #333333\"> let\u00a0notificationSettings: UIUserNotificationSettings! = UIApplication.shared.currentUserNotificationSettings<\/span><br \/>\n<span style=\"color: #333333\"> if\u00a0(notificationSettings.types == UIUserNotificationType()){<\/span><br \/>\n<span style=\"color: #333333\"> \/\/ Specify the notification types.<\/span><br \/>\n<span style=\"color: #333333\"> var\u00a0notificationTypes: UIUserNotificationType = [.alert, .sound]<\/span><br \/>\n<span style=\"color: #333333\"> \/\/ Specify the notification actions.<\/span><br \/>\n<span style=\"color: #333333\"> var\u00a0justInformAction = UIMutableUserNotificationAction()<\/span><br \/>\n<span style=\"color: #333333\"> justInformAction.identifier =\u00a0&#8220;justInform&#8221;<\/span><br \/>\n<span style=\"color: #333333\"> justInformAction.title =\u00a0&#8220;OK, got it&#8221;<\/span><br \/>\n<span style=\"color: #333333\"> justInformAction.activationMode = UIUserNotificationActivationMode.background<\/span><br \/>\n<span style=\"color: #333333\"> justInformAction.isDestructive =\u00a0false<\/span><br \/>\n<span style=\"color: #333333\"> justInformAction.isAuthenticationRequired =\u00a0false<\/span><br \/>\n<span style=\"color: #333333\"> var\u00a0modifyListAction = UIMutableUserNotificationAction()<\/span><br \/>\n<span style=\"color: #333333\"> modifyListAction.identifier =\u00a0&#8220;editList&#8221;<\/span><br \/>\n<span style=\"color: #333333\"> modifyListAction.title =\u00a0&#8220;Edit list&#8221;<\/span><br \/>\n<span style=\"color: #333333\"> modifyListAction.activationMode = UIUserNotificationActivationMode.foreground<\/span><br \/>\n<span style=\"color: #333333\"> modifyListAction.isDestructive =\u00a0false<\/span><br \/>\n<span style=\"color: #333333\"> modifyListAction.isAuthenticationRequired =\u00a0true<\/span><br \/>\n<span style=\"color: #333333\"> var\u00a0trashAction = UIMutableUserNotificationAction()<\/span><br \/>\n<span style=\"color: #333333\"> trashAction.identifier =\u00a0&#8220;trashAction&#8221;<\/span><br \/>\n<span style=\"color: #333333\"> trashAction.title =\u00a0&#8220;Delete list&#8221;<\/span><br \/>\n<span style=\"color: #333333\"> trashAction.activationMode = UIUserNotificationActivationMode.background<\/span><br \/>\n<span style=\"color: #333333\"> trashAction.isDestructive =\u00a0true<\/span><br \/>\n<span style=\"color: #333333\"> trashAction.isAuthenticationRequired =\u00a0true<\/span><br \/>\n<span style=\"color: #333333\"> let\u00a0actionsArray = NSArray(objects: justInformAction, modifyListAction, trashAction)<\/span><br \/>\n<span style=\"color: #333333\"> let\u00a0actionsArrayMinimal = NSArray(objects: trashAction, modifyListAction)<\/span><br \/>\n<span style=\"color: #333333\"> \/\/ Specify the category related to the above actions.<\/span><br \/>\n<span style=\"color: #333333\"> var\u00a0shoppingListReminderCategory = UIMutableUserNotificationCategory()<\/span><br \/>\n<span style=\"color: #333333\"> shoppingListReminderCategory.identifier =\u00a0&#8220;shoppingListReminderCategory&#8221;<\/span><br \/>\n<span style=\"color: #333333\"> shoppingListReminderCategory.setActions(actionsArray\u00a0as! [UIUserNotificationAction],\u00a0for: UIUserNotificationActionContext.default)<\/span><br \/>\n<span style=\"color: #333333\"> shoppingListReminderCategory.setActions(actionsArrayMinimal\u00a0as! [UIUserNotificationAction],\u00a0for: UIUserNotificationActionContext.minimal)<\/span><br \/>\n<span style=\"color: #333333\"> let\u00a0categoriesForSettings = NSSet(objects: shoppingListReminderCategory)<\/span><br \/>\n<span style=\"color: #333333\"> \/\/ Register the notification settings.<\/span><br \/>\n<span style=\"color: #333333\"> \/\/let newNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: categoriesForSettings)<\/span><br \/>\n<span style=\"color: #333333\"> let\u00a0newNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: categoriesForSettings\u00a0as! Set&lt;UIUserNotificationCategory&gt;)<\/span><br \/>\n<span style=\"color: #333333\"> UIApplication.shared.registerUserNotificationSettings(newNotificationSettings)<\/span><br \/>\n<span style=\"color: #333333\"> }<\/span><br \/>\n<span style=\"color: #333333\"> }<\/span><\/p>\n<p><strong><span style=\"color: #333333\">Also add the below code snippet:<\/span><\/strong><\/p>\n<p><span style=\"color: #333333\">func\u00a0scheduleLocalNotification() {<\/span><br \/>\n<span style=\"color: #333333\"> let\u00a0localNotification = UILocalNotification()<\/span><br \/>\n<span style=\"color: #333333\"> localNotification.fireDate = fixNotificationDate(datePicker.date)<\/span><br \/>\n<span style=\"color: #333333\"> localNotification.alertBody =\u00a0&#8220;Hey, you have to go shopping!!&#8221;<\/span><br \/>\n<span style=\"color: #333333\"> localNotification.alertAction =\u00a0&#8220;View List&#8221;<\/span><br \/>\n<span style=\"color: #333333\"> localNotification.category =\u00a0&#8220;shoppingListReminderCategory&#8221;<\/span><br \/>\n<span style=\"color: #333333\"> UIApplication.shared.scheduleLocalNotification(localNotification)<\/span><br \/>\n<span style=\"color: #333333\"> }<\/span><\/p>\n<p><span style=\"color: #333333\">This is the text which will be shown on top when reminder notification is received.\u00a0In this, we add the localNotification category so that it can be identified as the interactive notification.<\/span><\/p>\n<p><span style=\"color: #333333\">In the\u00a0setupNotificationSettings method, we will be adding different actions that can be performed by a user when swiping on the notification banner.<\/span><\/p>\n<p><span style=\"color: #333333\">Now call setupNotificationSettings method in your <\/span>viewdidload<span style=\"color: #333333\"> of <\/span>view<span style=\"color: #333333\"> controller.<\/span><\/p>\n<p><span style=\"color: #333333\">Now when you build your application:<\/span><\/p>\n<p><span style=\"color: #333333\"><strong>Adding Items to List<\/strong><\/span><\/p>\n<p align=\"LEFT\"><span style=\"color: #333333\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-45820\" src=\"\/blog\/wp-ttn-blog\/uploads\/2017\/02\/Screen-Shot-2017-01-31-at-1.33.45-PM-168x300.png\" alt=\"Screen Shot 2017-01-31 at 1.33.45 PM\" width=\"168\" height=\"300\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2017\/02\/Screen-Shot-2017-01-31-at-1.33.45-PM-168x300.png 168w, \/blog\/wp-ttn-blog\/uploads\/2017\/02\/Screen-Shot-2017-01-31-at-1.33.45-PM-575x1024.png 575w, \/blog\/wp-ttn-blog\/uploads\/2017\/02\/Screen-Shot-2017-01-31-at-1.33.45-PM-624x1109.png 624w, \/blog\/wp-ttn-blog\/uploads\/2017\/02\/Screen-Shot-2017-01-31-at-1.33.45-PM.png 750w\" sizes=\"(max-width: 168px) 100vw, 168px\" \/><\/span><\/p>\n<p><span style=\"color: #333333\"><strong>Schedule Reminder<\/strong><\/span><\/p>\n<p align=\"LEFT\"><span style=\"color: #333333\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-45821\" style=\"font-size: 1rem\" src=\"\/blog\/wp-ttn-blog\/uploads\/2017\/02\/Screen-Shot-2017-01-31-at-1.33.52-PM.png\" alt=\"Screen Shot 2017-01-31 at 1.33.52 PM\" width=\"100\" height=\"178\" \/><\/span><\/p>\n<p><span style=\"color: #333333\"><strong>Now let&#8217;s go to the Home Page when the notification arrives:<\/strong><\/span><\/p>\n<p align=\"LEFT\"><span style=\"color: #333333\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-45822\" style=\"font-size: 1rem\" src=\"\/blog\/wp-ttn-blog\/uploads\/2017\/02\/Screen-Shot-2017-01-31-at-1.35.12-PM.png\" alt=\"Screen Shot 2017-01-31 at 1.35.12 PM\" width=\"100\" height=\"178\" \/><\/span><\/p>\n<p><span style=\"color: #333333\"><strong>On a swipe, we will be shown different options to choose from.<\/strong><\/span><\/p>\n<p align=\"LEFT\"><span style=\"color: #333333\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-45824\" style=\"font-size: 1rem\" src=\"\/blog\/wp-ttn-blog\/uploads\/2017\/02\/Screen-Shot-2017-01-31-at-1.54.04-PM-182x300.png\" alt=\"Screen Shot 2017-01-31 at 1.54.04 PM\" width=\"182\" height=\"300\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2017\/02\/Screen-Shot-2017-01-31-at-1.54.04-PM-182x300.png 182w, \/blog\/wp-ttn-blog\/uploads\/2017\/02\/Screen-Shot-2017-01-31-at-1.54.04-PM.png 487w\" sizes=\"(max-width: 182px) 100vw, 182px\" \/><\/span><\/p>\n<p align=\"LEFT\"><span style=\"color: #333333\">You can now easily use the above steps to integrate the Interactive Notifications on iOS application.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Receiving and managing notifications on iOS\u00a0is a nightmare until the introduction of Notification Centre in iOS 5. Android long held an advantage over iOS in terms of how easily users could interact. However,\u00a0Apple has evolved how its premiere OS presents notification from third-party apps, but has limited developers to title and string of text. Interactive [&hellip;]<\/p>\n","protected":false},"author":827,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":39},"categories":[1400,1772,1],"tags":[4418,4848,3207],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/45896"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/827"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=45896"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/45896\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=45896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=45896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=45896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}