Reputation: 61
i created a dictionary with a date(key) and words (First - Second ..) like this :
let dict = ["23.08.2021":["First","number"],
"24.08.2021":["second","number"],
"25.08.2021":["third","number"]]
i want the app send a local notifcation depneding on the currint date of the app ! if today is 25.08.2021 the app will send local notifcation with the word third as a title .
What i did :
to get the currnt date i use this code :
let date3 = Date()
let thirtyDay = Calendar.current.date(byAdding: .day, value: 0, to: date3)!
let formatter3 = DateFormatter()
formatter3.dateFormat = "dd.MM.yyyy"
let result3 = formatter3.string(from: thirtyDay)
UserDefaults.standard.set(result3, forKey:"nextDAY")
to save the key and the word from the dictionary :
for (key, value) in dict {
UserDefaults.standard.set(value[0], forKey:"the number")
UserDefaults.standard.set(value[1], forKey:"the word")
}
code :
func scheduleLocal() {
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = " #### what should i write here??? #######"
content.body = "click here"
content.categoryIdentifier = "alarm"
content.userInfo = ["customData": "fizzbuzz"]
content.sound = UNNotificationSound.default
TimeSet.datePickerMode = .time
let date = TimeSet.date
let components = Calendar.current.dateComponents([.hour, .minute], from: date)
let hour = components.hour!
let minute = components.minute!
var dateComponents = DateComponents()
dateComponents.hour = hour
dateComponents.minute = minute
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: "X", content: content, trigger: trigger)
center.add(request)
}
Upvotes: 0
Views: 202
Reputation: 36294
If you want your app to send a local notification depending on the current date using your "scheduleLocal()" function,try something like this:
func scheduleLocal() {
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
let formatter = DateFormatter()
formatter.dateFormat = "dd.MM.yyyy"
let todayKey = formatter.string(from: Date())
if let title = dict[todayKey]?[0] {
content.title = title
} else {
// content.title = "what ever you want to send"
// or don't send anything
return
}
content.body = "click here"
content.categoryIdentifier = "alarm"
content.userInfo = ["customData": "fizzbuzz"]
content.sound = UNNotificationSound.default
TimeSet.datePickerMode = .time
let date = TimeSet.date
let components = Calendar.current.dateComponents([.hour, .minute], from: date)
let hour = components.hour!
let minute = components.minute!
var dateComponents = DateComponents()
dateComponents.hour = hour
dateComponents.minute = minute
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: "X", content: content, trigger: trigger)
center.add(request)
}
PS: your "thirtyDay" should probably be like this:
let thirtyDay = Calendar.current.date(byAdding: .day, value: 30, to: date3)!
Upvotes: 1