我有时间选择器设置。本地通知需要时间从选择器和发送通知。
我有三个通知标识符notfica2,notifica3和notifica 4。
现在,我希望每个工作日(周一至周五)重复通知2和notifica3,并在每个周末(周六至周日)重复通知4。
我没有为通知选择日期。所以我认为timePicker会自动选择设置时间的日期。
请告诉我如何实施此特定重复选项。
另外,我还有另一个错误。让我们说,当我选择时间是下午5点。我想在下午3点设置notifica2和notifica3(我想在每个工作日重复)
例外是负面时间。我理解这个问题,但我需要修复。所以,如果我下午3点但已经下午6点,那么下午3点通知将在第二天发送,如果不是周六或周日。与notifica相同4.但相反,这将在下周六转移5天。
到目前为止,这是我的代码。 这只是较大应用程序的一小部分。
var textfield:UITextField!
let timePicker = UIDatePicker()
let timePicker2 = UIDatePicker()
let timePicker3 = UIDatePicker()
func timePickerFunction(){
//toolbar
timePicker.datePickerMode = UIDatePickerMode.time
timePicker2.datePickerMode = UIDatePickerMode.time
timePicker3.datePickerMode = UIDatePickerMode.time
let toolBar = UIToolbar()
toolBar.sizeToFit()
//DoneButton
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector (donePressed))
toolBar.setItems([doneButton], animated: false)
BWTimePicker.inputAccessoryView = toolBar
AWTimePicker.inputAccessoryView = toolBar
ODTimePicker.inputAccessoryView = toolBar
//Assigning timepicker to the text fields
BWTimePicker.inputView = timePicker
AWTimePicker.inputView = timePicker2
ODTimePicker.inputView = timePicker3
}
func donePressed(){
//Formatting the date.
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .none
dateFormatter.timeStyle = .short
// BWTimePicker.text = "\(timePicker.date)"
// AWTimePicker.text = "\(timePicker.date)"
// ODTimePicker.text = "\(timePicker.date)"
// self.view.endEditing(true)
if BWTimePicker.isEditing {
//do something to textfield1
BWTimePicker.text = dateFormatter.string(from: timePicker.date)
self.view.endEditing(true)
}
else if AWTimePicker.isEditing {
//do something to textfield2
AWTimePicker.text = dateFormatter.string(from: timePicker2.date)
self.view.endEditing(true)
}
else if ODTimePicker.isEditing {
//do something to textfield2
ODTimePicker.text = dateFormatter.string(from: timePicker3.date)
self.view.endEditing(true)
}
// self.view.endEditing(true)
}
@IBAction func updateTime(_ sender: UIButton) {
//1. Get text from three UITextField objects.
let stringBW = BWTimePicker.text
let stringAW = AWTimePicker.text
let stringOD = ODTimePicker.text
//2. If the three time fields are not empty , we would send the notificaiton
if !(stringBW?.isEmpty)! && !(stringAW?.isEmpty)! && !(stringOD?.isEmpty)! {
//3. We would create a dictionary object which would be send in the notification
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "Hi!", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "Your Satisfier Quote is Ready", arguments: nil)
content.categoryIdentifier = "time-test"
content.sound = UNNotificationSound.default()
var timeInterval1: Double = 0
var timeInterval2: Double = 0
var timeInterval3: Double = 0
let date = timePicker.date
let date2 = timePicker2.date
let date3 = timePicker3.date
timeInterval1 = date.timeIntervalSinceNow
timeInterval2 = date2.timeIntervalSinceNow
timeInterval3 = date3.timeIntervalSinceNow
print("HMMMMMMMMMKMKMKMKSMKDMSKDMSK")
print(timeInterval1)
print(timeInterval2)
print(timeInterval3)
setNotification(timeInterval:timeInterval1,identifier: "notific2", date: date)
setNotification(timeInterval:timeInterval2,identifier: "notific3", date: date2)
setNotification(timeInterval:timeInterval3,identifier: "notific4", date: date3)
self.isProfileViewVisible = true
self.extraViewTopConstraint2.constant = 1000
self.profileViewTopConstraint.constant = 150
}
else{
//2. Ask user to enter text in all the fields.
self.isProfileViewVisible = true
self.extraViewTopConstraint2.constant = 1000
self.profileViewTopConstraint.constant = 150
}
}
func setNotification (timeInterval : Double, identifier : String, date : Date){
let A: Int = 0
let B: Int = codeTwo.count
let C: Int = codeOne.count
let D: Int = codeThree.count
let number1 = Int (arc4random_uniform(UInt32(B - 1)))
let number2 = Int (arc4random_uniform(UInt32(C - 1)))
let number3 = Int (arc4random_uniform(UInt32(D - 1)))
//Repeat fundtion only added to notifica 2 for experimaental purpose/
if(identifier == "notific2"){
let content: UILocalNotification = UILocalNotification()
content.category = NSString.localizedUserNotificationString(forKey: "Hey there!", arguments: nil)
content.alertBody = NSString.localizedUserNotificationString(forKey: "\(codeTwo[number1])", arguments: nil)
//content.categoryIdentifier = "time-test"
content.soundName = UILocalNotificationDefaultSoundName
let time = date
content.fireDate = time
content.repeatInterval = NSCalendar.Unit.day
// let triggerDate = Calendar.current.dateComponents([.weekday,.hour,.minute], from: time as Date)
//
// let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate,
// repeats: true)
//
// //let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: timeInterval, repeats: true)
// let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger)
//
// let center = UNUserNotificationCenter.current()
// center.add(request)
UIApplication.shared.scheduleLocalNotification(content)
}
if(identifier == "notific3"){
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "Hey there!", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "\(codeThree[number3])", arguments: nil)
//content.categoryIdentifier = "time-test"
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: timeInterval, repeats: true)
let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request)
}
if(identifier == "notific4"){
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "Hey there!", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "\(codeOne[number2])", arguments: nil)
//content.categoryIdentifier = "time-test"
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: timeInterval, repeats: true)
let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request)
}
}
//MARK: ViewController lifeCycle
override func viewDidLoad() {
super.viewDidLoad()
self.customization()
self.collectionView.allowsMultipleSelection = true
searchBar.delegate = self
//searchBar.returnKeyType = UIReturnKeyType.done
items = items1
self.navigationController?.isNavigationBarHidden = true
timePickerFunction()
// (sender as AnyObject).setTitle("Add to Group", for: [])
UNUserNotificationCenter.current().requestAuthorization(
options: [.alert,.sound])
{
(granted, error) in
self.notificationGranted = granted
if let error = error {
print("granted, but Error in notification permission:\(error.localizedDescription)")
}
}
}