Skip to main content

app-ads.txt

 google.com, pub-2203413632152319, DIRECT, f08c47fec0942fa0

Push Notifications Service - Local Push Notifications Swift 5

How to create local notifications in iOS using swift 5 on click a custom Button. Local notifications are most commonly used in apps like calendar alert, alarm app set event and more Alert with a customized message, a to do reminder etc. So let us start and learn how we can integrate local notification in iOS using swift language. Follow below steps, to integrate local notifications in swift. 

Push Notifications Service - Local Push Notifications Swift 5


Local and Remote Notification Programming Guide - Local notifications and remote notifications are ways to inform users when new data becomes available for your app - Local and Remote Notifications Overview


Local Notifications: Now Getting Started:- 


Step 1:  


import UserNotifications

 

Step 2:


    override func viewDidLoad() {

        super.viewDidLoad()

        // Configure User Notification Center

            UNUserNotificationCenter.current().delegate = self

    }


Step 3:


@IBAction func didLocalNotification(_ sender: Any) {

        // Request to notification settings

        UNUserNotificationCenter.current().getNotificationSettings { (notificationSettings) in

            switch notificationSettings.authorizationStatus {

                case .notDetermined:

                    self.requestAuthorization(completionHandler: { (success) in

                        guard success else { return }

                        self.getScheduleLocalNotification()  // Schedule Local Notification

                    })

                    // Request Authorization

                    break

                case .authorized:

                       self.getScheduleLocalNotification()  // Schedule Local Notification

                    break

                case .denied:

                    print(" Not Allowed to Display Notifications- Application")

                case .provisional:

                    break

                case .ephemeral:

                    break

                @unknown default:

                    break

            }

        }

    }

 

Step 4: // Request For Authorization

 

private func requestAuthorization(completionHandler: @escaping (_ success: Bool) -> ()) {

        // Request Authorization

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

            if let error = error {

                print("Request Authorization Failed (\(error), \(error.localizedDescription))")

            }

            completionHandler(success)

        }

    }

 

Step 5:

 

    private func scheduleLocalNotification() {

        // Create Notification Content

        let notificationContent = UNMutableNotificationContent()

 

        // Configure Notification Content

        notificationContent.title = “iOS Developer live"

        notificationContent.subtitle = "Local Notifications"

        notificationContent.body = "this tutorial, local notifications with the User Notifications framework. & how to schedule "

 

        // Add Time Interval Trigger

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

 

        // Create Request for Notification

        let notificationRequest = UNNotificationRequest(identifier: "cocoacasts_local_notification", content: notificationContent, trigger: notificationTrigger)

 

        // Add Request to User Notification Center

        UNUserNotificationCenter.current().add(notificationRequest) { (error) in

            if let error = error {

                print("Unable to Add Notification Request (\(error), \(error.localizedDescription))")

            }

        }

    }

 

Step 6:

 

extension UIViewController: UNUserNotificationCenterDelegate {

 

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

        completionHandler([.alert])

    }

 

}


Comments

Post a Comment