R
R
Rodion1597532020-07-09 11:24:02
Swift
Rodion159753, 2020-07-09 11:24:02

How to make multiple local notifications in Swift?

The bottom line is that I made one local notification at a time interval of 10 seconds.
How to add multiple notifications to this code with different time delay? That is, different notifications with different delays (several hours, days, etc.).
If it is possible, then on the example of my code, please.

import UIkit
import UserNotifications

class ViewController UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let center = UNUserNotificationCenter.current()
        
        let content = UNMutableNotificationContent()
        
        content.title = "Hi"
        content.body = "Bear"
        content.sound = UNNotificationSound.default
        content.threadIdentifier = "local-notifications"
        
        let date = Date(timeIntervalSinceNow: 10)
        
        let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: date)
        
        let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
        
        let request = UNNotificationRequest(identifier: "content", content: content, trigger: trigger)
        
        center.add(request) {(error) in 
            if error != nil {
                print (error)
            }
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Vorobei, 2020-07-09
@ivanvorobei

Put different and different time. PS I strongly do not recommend copy-pasting the code, understand how it works - then such questions will not appear. And you will know more. identifier: "content"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question