swift - 如何使用 Swift 向用户工作日发送本地通知,其中包含不同的文本
问题描述
想根据日期名称在所有日子里向用户发送通知,例如,如果今天是星期三,小时是下午 4 点,发送通知“今天是星期三,你好做点什么”,那天之后我的意思是星期四,时钟是下午 4 点发送通知,例如“你好,星期四做你的运动”等等,你能帮我吗
public struct NotificationManager {
let notificationCenter = UNUserNotificationCenter.current()
static let sharedInstance = NotificationManager()
private func checkRequestAuthorization(completion: @escaping(Bool) -> Void) -> () {
let options: UNAuthorizationOptions = [.alert, .sound,]
notificationCenter.requestAuthorization(options: options) {
(didAllow, error) in
completion(didAllow)
}
}
func setLocalNotifications() {
checkRequestAuthorization { isAllowed in
if isAllowed {
let credentials = getNotificationsCredentials()
addNotificationRequest(title: Localizables.InformationTitles.notificationTitle, body: Localizables.InformationTexts.notificationBody, notifications: credentials)
}
}
}
private func getNotificationsCredentials() -> [CustomNotification] {
return [ CustomNotification(hours: [8,16], title: ["Smiling Sunday", "Self Sunday"], body: ["What makes you smile?", "Let’s take some time to draw today!"]),
CustomNotification(hours: [16], title: ["Mindful Monday"], body: ["Let’s nn today!"]),
CustomNotification(hours: [16], title: ["Thoughtful Tuesday"], body: ["Come and play!"]),
CustomNotification(hours: [16], title: ["Wishful Wednesday"], body: ["Let’s make a wish today!"]),
CustomNotification(hours: [16], title: ["Thankful Thursday"], body: ["What are you thankful for today?"]),
CustomNotification(hours: [16], title: ["Feeling Friday"], body: ["Let’s explore our feelings together!"]),
CustomNotification(hours: [8,16], title: ["Super Saturday", "Singing Saturday"], body: ["What are you super excited for today?", "Let’s dance and sing together!"]),
]
}
private func addNotificationRequest(title: String, body: String, notifications: [CustomNotification]) {
notificationCenter.removeAllPendingNotificationRequests()
let selectedNotification = notifications[Date().dayNumberOfWeek()!-1]
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.sound = UNNotificationSound.default
let gregorian = Calendar(identifier: Calendar.Identifier.gregorian)
let now = Date()
var components = gregorian.dateComponents(in: .autoupdatingCurrent, from: now)
let hours = [14,18]
for hour in hours {
components.timeZone = TimeZone.current
components.hour = hour
components.minute = 28
components.second = 00
let date = gregorian.date(from: components);
let formatter = DateFormatter();
formatter.dateFormat = "MM-dd-yyyy HH:mm";
let dailyTrigger = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: date!);
let trigger = UNCalendarNotificationTrigger(dateMatching: dailyTrigger, repeats: true);
let identifier = UUID().uuidString
let reguest = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
notificationCenter.add(reguest) { err in
print("notification fired")
}
}
}
}
解决方案
我发现它我刚刚创建了所有通知,并使用 for 循环我请求所有通知,而 datetimertriger 完成了这项工作
public struct NotificationManager {
let notificationCenter = UNUserNotificationCenter.current()
static let sharedInstance = NotificationManager()
private func checkRequestAuthorization(completion: @escaping(Bool) -> Void) -> () {
let options: UNAuthorizationOptions = [.alert, .sound,]
notificationCenter.requestAuthorization(options: options) {
(didAllow, error) in
completion(didAllow)
}
}
func setLocalNotifications() {
checkRequestAuthorization { isAllowed in
if isAllowed {
let credentials = getNotificationsCredentials()
addNotificationRequest(title: Localizables.InformationTitles.notificationTitle, body: Localizables.InformationTexts.notificationBody, notifications: credentials)
}
}
}
private func getNotificationsCredentials() -> [CustomNotification] {
return [
CustomNotification(hours: 8, title: "Smiling Sunday", body: "What makes you smile?", weekDay: 1),
CustomNotification(hours: 16, title: "Self Sunday", body: "Let’s take some time to draw today!", weekDay: 1),
CustomNotification(hours: 16, title: "Mindful Monday", body: "Let’s tinker today!",weekDay: 2),
CustomNotification(hours: 16, title: "Thoughtful Tuesday", body: "Come and play!",weekDay: 3),
CustomNotification(hours: 16, title: "Wishful Wednesday", body: "Let’s make a wish today!",weekDay: 4),
CustomNotification(hours: 16, title: "Thankful Thursday", body: "What are you thankful for today?",weekDay: 5),
CustomNotification(hours: 16, title: "Feeling Friday,", body: "Let’s explore our feelings together!",weekDay: 6),
CustomNotification(hours: 8, title: "Super Saturday", body: "What are you super excited for today?",weekDay: 7),
CustomNotification(hours: 16, title: "Singing Saturday,", body: "Let’s dance and sing together!",weekDay: 7),
]
}
private func addNotificationRequest(title: String, body: String, notifications: [CustomNotification]) {
notificationCenter.removeAllPendingNotificationRequests()
// let gregorian = Calendar(identifier: Calendar.Identifier.gregorian)
// let now = Date()
for notification in notifications {
let content = UNMutableNotificationContent()
content.title = notification.title
content.body = notification.body
content.sound = UNNotificationSound.default
var dateComponents = DateComponents()
dateComponents.timeZone = TimeZone.current
dateComponents.hour = notification.hours
dateComponents.minute = 10
dateComponents.second = 00
dateComponents.weekday = notification.weekDay
//let date = gregorian.date(from: dateComponents);
let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
// let formatter = DateFormatter();
// formatter.dateFormat = "MM-dd-yyyy HH:mm";
// let dailyTrigger = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: date!);
// let trigger = UNCalendarNotificationTrigger(dateMatching: dailyTrigger, repeats: true);
let identifier = UUID().uuidString
let reguest = UNNotificationRequest(identifier: identifier, content: content, trigger: notificationTrigger)
notificationCenter.add(reguest) { err in
}
}
}
}
推荐阅读
- excel - 将单元格/表格从 Excel 复制并粘贴到 Word 模板(以及正确的位置)
- angular - IONIC 4 中的 LTR 和 RTL
- class - Delphi 10.2 - 无法解析单元名称“控件”和“对话框”
- wordpress - 如何使特定的滑块作为滑块旋转中的默认滑块
- javascript - 反应 | 蚂蚁设计选择默认值
- mysql - 透视mysql表返回
- android - 为 RTP 流提供 SDP 格式的描述
- php - 本地与部署的 Laravel 问题
- javascript - Chrome 以外的浏览器中的 JSON.parse 错误
- python - 在 Python3 中将 unicode 序列转换为字符串,但允许字符串中的路径