Local Notification App Swift5 iOS

 


Download Demo Code From Here πŸ‘‡πŸ»

https://gitlab.com/hiren_syl/local-notifications




let userNotificationCenter = UNUserNotificationCenter.current()
&&&
override func viewDidLoad() {
    super.viewDidLoad()
    self.requestNotificationAuthorization()
    self.sendNotification()
}
&&&
let authOptions = UNAuthorizationOptions.init(arrayLiteral: .alert, .badge, .sound)
&&&
func requestNotificationAuthorization() {
    let authOptions = UNAuthorizationOptions.init(arrayLiteral: .alert, .badge, .sound)
    
    self.userNotificationCenter.requestAuthorization(options: authOptions) { (success, error) in
        if let error = error {
            print("Error: ", error)
        }
    }
}
&&&
// Create new notifcation content instance
let notificationContent = UNMutableNotificationContent()

// Add the content to the notification content
notificationContent.title = "Test"
notificationContent.body = "Test body"
notificationContent.badge = NSNumber(value: 3)

// Add an attachment to the notification content
if let url = Bundle.main.url(forResource: "dune",
                                withExtension: "png") {
    if let attachment = try? UNNotificationAttachment(identifier: "dune",
                                                        url: url,
                                                        options: nil) {
        notificationContent.attachments = [attachment]
    }
}
&&&
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5,
                                                repeats: false)
&&&
let request = UNNotificationRequest(identifier: "testNotification",
                                    content: notificationContent,
                                    trigger: trigger)
&&&
userNotificationCenter.add(request) { (error) in
    if let error = error {
        print("Notification Error: ", error)
    }
}


final code:- 

func sendNotification() {
    let notificationContent = UNMutableNotificationContent()
    notificationContent.title = "Test"
    notificationContent.body = "Test body"
    notificationContent.badge = NSNumber(value: 3)
    
    if let url = Bundle.main.url(forResource: "dune",
                                withExtension: "png") {
        if let attachment = try? UNNotificationAttachment(identifier: "dune",
                                                        url: url,
                                                        options: nil) {
            notificationContent.attachments = [attachment]
        }
    }
    
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5,
                                                    repeats: false)
    let request = UNNotificationRequest(identifier: "testNotification",
                                        content: notificationContent,
                                        trigger: trigger)
    
    userNotificationCenter.add(request) { (error) in
        if let error = error {
            print("Notification Error: ", error)
        }
    }
}

handle notification:-
// Local notifications
func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
    UIApplication.shared.applicationIconBadgeNumber = 0
}

Comments

Popular posts from this blog

Learn Swift Programming in 1 Day

Firebase Demo Code with Swift5

MUSIC PLAYER - PLAY, PAUSE, NEXT, BACK #Swift5 #XCode