How to Make Interactive iOS 10 Notifications With User Notifications Framework

0
Shares
How to Make Interactive iOS 10 Notifications With User Notifications Framework
1 (20%) 2 votes

Up till now, we all have been using iOS push notifications to send alerts to app users. And honestly, it worked just fine. But, the new iOS 10 notifications have been taken to the next level. The new User Notifications framework allows us to embed custom view controller in iOS notifications. This will result into more powerful iOS 10 notifications, that were possible possible to create before.

In this iOS 10 tutorial, we’ll cover the basic text iOS 10 notifications and making it interactive with like/dislike action button.

Let’s Get Started

Create a new project under file menu and select “Single View Application”.

sousernotifications1

In the next tab, customize your app details and click on finish.

sousernotification2

Now, import the new notifications framework in ViewController.swift

// Swift

import UserNotifications

Get permission for notification and call it in viewDidLoad()

override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        UNUserNotificationCenter.current().delegate = self

        

        authorizeNotification()

    }

    

    

    //Authorization for Local Notification

    func authorizeNotification() {

        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (success, error) in

            if let error = error {

                print("Error:- \(error)")

            } else if success == true {

                print("Permission Granted")

            }

        }

    }

sousernotification3

Once permission granted then create a click event scheduleNotification() for schedule notification.

@IBAction func scheduleNotification() {

                // Adding Action

        let likeAction = UNNotificationAction(identifier: "LikeID",

                                                title: "Like", options: [.foreground])

        let dislikeAction = UNNotificationAction(identifier: "DislikeID",

                                                title: "Dislike", options: [.foreground])

        

        let category = UNNotificationCategory(identifier: "app.likedislike.ios10",

                                              actions: [likeAction, dislikeAction],

                                              intentIdentifiers: [], options: [])

        UNUserNotificationCenter.current().setNotificationCategories([category])

         // Create Notification Content

        let notificationContent = UNMutableNotificationContent()

        

        // Configure Notification Content

        notificationContent.title = "iOS 10 UserNotifications"

        notificationContent.subtitle = "Local Notifications"

        notificationContent.body = "The UserNotifications framework (UserNotifications.framework) supports the delivery and handling of local and remote notifications."

        notificationContent.categoryIdentifier = "app.likedislike.ios10"

        

        // Add Trigger

        let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 10.0, repeats: false)

        // Create Notification Request

        let request = UNNotificationRequest(identifier: "app.likedislike.ios10",

                                            content: notificationContent, trigger: notificationTrigger)

        UNUserNotificationCenter.current().add(request, withCompletionHandler: { (error) in

            if let error = error {

                print("Error \(error)")

                // Something went wrong

            }

        })

    }

And done!

Now if you run the demo and schedule a notification to test, it will look like this:

By implementing this framework, you can now make interactive notifications in your iOS app. And this was a simple demo, but it is possible to make notifications even more interactional by embedding images or certain actions into the iOS 10 notifications. And if you need technical assistance, you can hire Swift developer from us to upgrade UI/UX of your mobile application.

Grab a free copy of User Notifications Framework Demo from Github.

This page was last edited on November 21st, 2016, at 15:30.
 
0
Shares
 

Want To Develop Interactive iOS App in Swift? Contact Us Now

Get your free consultation now