我有一个提醒应用程序,目前,如果我删除一个提醒,它也删除通知,如果提醒有一个日期或时间,你可以在下面的LocalNotificationManager看到它。现在,我还想删除一个提醒,如果用户选中提醒,但不删除它,您可以在我的reminderView中看到这一点,在我的ontapGesture中,但是如果他们再次检查它,将它设置为未完成,我需要它重新添加相同的通知请求,这样通知就会发出。下面有LocalNotificationManager和reminderView代码以及我的提醒模型。任何帮助都会很好!
提醒模型
“”“
struct Reminder: Identifiable, Equatable {
var title: String
var notes: String?
var date: Date?
var time: Date?
var theme: Theme
var iscomplete: Bool
var priority: RemindPriority
let id: UUID
//let filter: FilterType
init(title: String, notes: String? = nil, date: Date? = nil, time: Date? = nil, theme: Theme, iscomplete: Bool = false, priority: RemindPriority = .None, id: UUID = UUID()) {
self.title = title
self.notes = notes
self.date = date
self.time = time
self.theme = theme
self.iscomplete = iscomplete
self.priority = priority
self.id = id
}
/*var filteredReminders: [Reminder] {
switch filter {
case .all:
return HomeViewModel().reminds
case .today:
return HomeViewModel().reminds.filter($0.date.)
}
}*/
func formatDate(date: Date) -> String {
let formatter = DateFormatter()
formatter.dateStyle = .full
formatter.timeStyle = .none
return formatter.string(from: date)
}
func formatTime(time: Date) -> String {
let formatter = DateFormatter()
formatter.dateStyle = .none
formatter.timeStyle = .short
return formatter.string(from: time)
}
}“”“
ReminderView
“”“
import SwiftUI
struct ReminderView: View {
@ObservedObject var remindVM: ReminderViewModel
init(remind: Binding<Reminder>) {
remindVM = ReminderViewModel(remind: remind)
}
var body: some View {
VStack {
HStack {
Image(systemName: remindVM.remind.iscomplete ? "circle.fill" : "circle")
.font(.title)
.onTapGesture {
remindVM.remind.iscomplete.toggle()
if remindVM.remind.iscomplete {
LocalNotificationManager().deleteLocalNotifications(identifiers: [remindVM.remind.id.uuidString])
} else {
}
}
Text(remindVM.remind.priority.name)
.font(.title)
.foregroundColor(remindVM.remind.theme.accentColor)
if !remindVM.remind.iscomplete {
Text(remindVM.remind.title)
.font(.title)
} else {
Text(remindVM.remind.title)
.font(.title)
.opacity(0.7)
}
Spacer()
}
}
}“”“
LocalNotificationManager
“”“
import Foundation
import SwiftUI
class LocalNotificationManager: ObservableObject {
@Published var deletedNotifications: [UNNotificationRequest] = []
init() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
if granted == true && error == nil {
print("Notifications are allowed")
} else {
print("Notifications are not allowed")
}
}
}
func sendNotification(date: Date, type: String, timeInterval: Double = 10, title: String, body: String, id: UUID) {
var trigger: UNNotificationTrigger?
if type == "date" {
let dateComponents = Calendar.current.dateComponents([.day, .month, .year, .hour, .minute], from: date)
trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
} else if type == "time" {
trigger = UNTimeIntervalNotificationTrigger(timeInterval: timeInterval, repeats: false)
}
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [id.uuidString])
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.sound = UNNotificationSound.default
//content.badge = (notifications.count) as NSNumber
let request = UNNotificationRequest(identifier: id.uuidString, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
//notifications.append(request)
}
func deleteLocalNotifications(identifiers: [String]) {
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: identifiers)
}
}“”“
发布于 2022-05-19 04:09:52
我想出来了!结果,我只需要在我的reminderView中的else语句中创建另一个发送通知请求。在“提醒”视图中的onGesture中,我还添加了一个if语句,该语句包含用于通知的if else语句,以检查通知是否有日期,否则删除不存在的通知是没有意义的。
https://stackoverflow.com/questions/72263656
复制相似问题