R
R
Rodion Bizyaev2020-03-28 11:56:27
iOS
Rodion Bizyaev, 2020-03-28 11:56:27

How to make local notifications scheduled in Swift?

Good afternoon.
The essence of the application: on the specified date and time, the application must "throw" local notifications with certain phrases. The phrases do not intersect, so the repetition will not work.
How to set a separate time and date for each phrase?
New to Swift. I'm trying to figure it out. But after I googled it, it's already a mess in my head.
According to the tutorials, I have now made a code that works, but it works with a delay of 10 seconds. That eats after pressing the button after 10 seconds a push is thrown.
How can I make it so that either I just set different delays (for example 3,6,9,12,15 hours for each phrase), or refactor the code to separate each notification?
I don't even need it to be at the push of a button. You just go into the application, accept a request for notifications - and they come by the hour.
The question may seem very stupid. Do not judge strictly. Attached the code.

import UIKit
import UserNotifications

class ViewController: UIViewController {
    
    @IBAction func sendNotification(sender: UIButton){
        scheduleNotification(inSeconds: 10) {(success) in
            if success {
                print("We send it")
            } else {
                print("Failed")
            }
        }
    }
    
    func scheduleNotification(inSeconds seconds:TimeInterval, completion: (Bool) -> ()) {
     
        removeNotification(withIdentifiers:["Bear"])
        
        let date = Date(timeIntervalSinceNow: seconds)
        print (Date())
        print(date)
        
        let content = UNMutableNotificationContent()
        content.title = "Текст"
        content.body = "Текст"
        content.sound = UNNotificationSound.default()
        
        let calendar = Calendar(identifier: .gregorian)
        let components = calendar.dateComponents([.month, .day, .hour, .minute, .second], from: date)
        let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false)
        let request = UNNotificationRequest(identifier: "Bear", content: content, trigger: trigger)
        
        let center = UNUserNotificationCenter.current()
        center.add(request, withCompletionHandler: nil)
        
    }
    
    
    
    
    
        
    func removeNotification(withIdentifiers identifiers: [String]) {
        let center = UNUserNotificationCenter.current()
        center.removePendingNotificationRequests(withIdentifiers: identifiers)
    }
    
    deinit {
        removeNotification(withIdentifiers: ["Bear"])
    }
    
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vyacheslav Beltyukov, 2020-03-30
@maestrro712

You have almost done everything that was needed. There are a few strokes left.
1. Now the function scheduleNotificationcreates notifications with the same id. New notifications overwrite the old ones. It is better to use either constants as id, or something related to the title: "\(content.title.hash)".
2. The text must be moved to the function parameters:

func scheduleNotification(inSeconds seconds:TimeInterval, text: String, completion: (Bool) -> ())
. Then it will be possible to use it to create a notification: content.title = text.
3. deinitmust be removed completely. It deletes your notification when the screen is removed from memory.
4. If you need to set notifications without pressing a button, then you can use the method viewDidLoad()from the same class.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question