Локальное уведомление несколько раз в день быстро - PullRequest
0 голосов
/ 03 августа 2020

Мне нужно получать несколько уведомлений в нужное время. Здесь, когда я вызвал функцию для планирования уведомления, я позвонил дважды, установив разное время, но я получил только одно уведомление, которое было последним. вот мой код

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
       
        let options: UNAuthorizationOptions = [.alert, .sound, .badge]
        notificationCenter.requestAuthorization(options: options) {
            (didAllow, error) in
            if !didAllow {
                print("User has declined notifications")
            }
        }
        registerForLocalNotifications()
        scheduleNotification(notificationType: "Test notif", time: "6:30 PM")
        scheduleNotification(notificationType: "Test notif", time: "6:31 PM")

        return true
    }

func scheduleNotification(notificationType: String,time: String) {
        
            let content = UNMutableNotificationContent()
            let startTime = time
            let fmt = DateFormatter()
            fmt.dateFormat = "h:mm a"
            let dateFrom = fmt.date(from: startTime)!
            content.title = notificationType
            content.body = "This is example how to create "
            content.sound = UNNotificationSound.default
            content.badge = 1
            let date = Date(timeIntervalSinceNow: 3600)
            let triggerDaily = Calendar.current.dateComponents([.hour,.minute,.second,], from: dateFrom)
            let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
            let identifier = "Local Notification"
            let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
            
            notificationCenter.add(request) { (error) in
                if let error = error {
                    print("Error \(error.localizedDescription)")
                }
            }
        
    }
...