How to implement multiple local notifications rather than one notification for quick 3

I use one notification and this is my code: this is for registering a local notification →>

func registerLocal() { let center = UNUserNotificationCenter.current() center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in if granted { print("Yay!") } else { print("D'oh") } } } 

and this function is designed to schedule local notification →>

 func scheduleLocal() { registerCategories() let center = UNUserNotificationCenter.current() // not required, but useful for testing! center.removeAllPendingNotificationRequests() let content = UNMutableNotificationContent() content.title = "good morning" content.body = "ttt123" content.categoryIdentifier = "alarm" content.userInfo = ["customData": "fizzbuzz"] content.sound = UNNotificationSound.default() var dateComponents = DateComponents() dateComponents.hour = 23 dateComponents.minute = 18 let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true) let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger) center.add(request) } func registerCategories() { let center = UNUserNotificationCenter.current() center.delegate = self let show = UNNotificationAction(identifier: "show", title: "Tell me more…", options: .foreground) let category = UNNotificationCategory(identifier: "alarm", actions: [show], intentIdentifiers: []) center.setNotificationCategories([category]) } func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { // pull out the buried userInfo dictionary let userInfo = response.notification.request.content.userInfo if let customData = userInfo["customData"] as? String { print("Custom data received: \(customData)") switch response.actionIdentifier { case UNNotificationDefaultActionIdentifier: // the user swiped to unlock; do nothing print("Default identifier") case "show": print("Show more information…") break default: break } } // you need to call the completion handler when you're done completionHandler() } 

now how can i use this code with multiple local notifications with iOS 10 and different times thanks.

+5
source share
4 answers

Use a different request identifier for each notification (otherwise you only see the last notification). In the example above, verify that the request identifier is “UUID (). UuidString” contains a unique value for each notification request.

+4
source

You can call func scheduleLocal() several times with different dateComponents to schedule for different dates. Or you can pass an array of dates to this function and start a loop to schedule notifications according to these dates.

Just make sure that the identifier you pass in the UNNotificationRequest(identifier:, content:, trigger:) function is different for each notification.

Hope this helps. :)

+4
source

Call this function with various parameters. As I called it when the app goes into wallpaper

  func applicationDidEnterBackground(_ application: UIApplication) { setNotification(time: 25,identifier: "notific2") setNotification(time: 50,identifier: "notific3") setNotification(time: 90,identifier: "notific4") } func setNotification (time : Int,identifier : String) { let content = UNMutableNotificationContent() content.title = "Don't forget" content.body = "Buy some milk" content.sound = UNNotificationSound.default() let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(time), repeats: false) let center = UNUserNotificationCenter.current() // Swift let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger) center.add(request, withCompletionHandler: { (error) in if error != nil { // Something went wrong } }) } 
+2
source

Please note that your code should work as expected! however, there is a little trap.

I think I almost ran into the same problem, after I tried to keep track of pending notifications, I noticed that the only notification that was added is the last requested. This is because you are calling the scheduleLocal() function:

 // not required, but useful for testing! center.removeAllPendingNotificationRequests() 

Ok, delete any existing reminder reminders, this will help prevent unnecessary recurring notifications, but you should only call it once before calling scheduleLocal() :

 UNUserNotificationCenter.current().removeAllPendingNotificationRequests() // now you could call this function many times... scheduleLocal() 

you just don’t want to delete every pending notification right after you add it, instead you delete all previous pending notifications and add new ones.

0
source

Source: https://habr.com/ru/post/1261434/


All Articles