本地通知更新为当前时间
local notification updated with current time
我创建了一个带有日期(键)和单词(第一 - 第二..)的字典,如下所示:
let dict = ["23.08.2021":["First","number"],
"24.08.2021":["second","number"],
"25.08.2021":["third","number"]]
我希望应用根据应用的当前日期发送本地通知!
如果今天是 2021 年 8 月 25 日,应用程序将发送本地通知,标题为 third。
我做了什么:
要获取当前日期,我使用此代码:
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")
从字典中保存关键字和单词:
for (key, value) in dict {
UserDefaults.standard.set(value[0], forKey:"the number")
UserDefaults.standard.set(value[1], forKey:"the word")
}
代码:
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)
}
如果您希望您的应用使用“scheduleLocal()”函数根据当前日期发送本地通知,请尝试如下操作:
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:你的“thirtyDay”应该是这样的:
let thirtyDay = Calendar.current.date(byAdding: .day, value: 30, to: date3)!
我创建了一个带有日期(键)和单词(第一 - 第二..)的字典,如下所示:
let dict = ["23.08.2021":["First","number"],
"24.08.2021":["second","number"],
"25.08.2021":["third","number"]]
我希望应用根据应用的当前日期发送本地通知! 如果今天是 2021 年 8 月 25 日,应用程序将发送本地通知,标题为 third。
我做了什么:
要获取当前日期,我使用此代码:
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")
从字典中保存关键字和单词:
for (key, value) in dict {
UserDefaults.standard.set(value[0], forKey:"the number")
UserDefaults.standard.set(value[1], forKey:"the word")
}
代码:
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)
}
如果您希望您的应用使用“scheduleLocal()”函数根据当前日期发送本地通知,请尝试如下操作:
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:你的“thirtyDay”应该是这样的:
let thirtyDay = Calendar.current.date(byAdding: .day, value: 30, to: date3)!