iOS Локальные уведомления - PullRequest
1 голос
/ 08 апреля 2020

Я пытаюсь запускать локальные уведомления на каждую 1 минуту, даже в завершенном и фоновом состоянии. Но мое приложение не должно вызывать уведомления, когда время после 18:00. Ниже мой код.

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60,repeats: true)
    //UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
    // Create the request
    let uuidString = UUID().uuidString
    let request = UNNotificationRequest(identifier: uuidString,
                content: content, trigger: trigger)

    // Schedule the request with the system.
    let notificationCenter = UNUserNotificationCenter.current()
    notificationCenter.add(request) { (error) in
       if error != nil {
          // Handle any errors.
       }
    }
}

1 Ответ

1 голос
/ 08 апреля 2020

Чтобы обработать локальное уведомление, попробуйте это:

public func addLocalNotification(hour: Int, minute: Int, identifier: String, title: String, body: String) {
    // Initialize
     let center = UNUserNotificationCenter.current()

   // Set its content
    let content = UNMutableNotificationContent()
    content.title = title
    content.body = body
    content.sound = .default

    // whenever you want to notify user select time
    var dateComp = DateComponents()
    dateComp.calendar = Calendar.current 
    dateComp.hour = hour
    dateComp.minute = minute

    // repeat and date
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComp, repeats: true)

    // Initializing the Notification Request object
    let req = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)

    // Add the notification to the center
    center.add(req) { (error) in
        if (error) != nil {
            print(error!.localizedDescription)
        }
    }
}
...