Swift中的单个项目的本地通知添加/删除
您好,我想为我的应用程序创建本地通知管理器,我的实体是用标题创建的通知一切都很好。 但是,例如今天是2022年6月1日,我为2022年6月5日创建了一个物品,到目前为止不用担心,但是如果我完成我的物品,我的到来仍然有效,我不明白字符串中的标识UUID工作……
这是下面我的代码,但是当我在另一个视图中调用cancel -Notification函数时,我不明白要确切地放置什么,item.ID,uuid,识别符? 我是编程的新手,谢谢
func pushNotification(input:Date,text:String){
let calendar = Calendar.current
let hour = calendar.component(.hour, from: input)
let minute = calendar.component(.minute, from: input)
let second = calendar.component(.second, from: input)
let day = calendar.component(.day, from: input)
let month = calendar.component(.month, from: input)
let content = UNMutableNotificationContent()
content.title = "Organized"
content.subtitle = text
content.sound = .default
content.badge = 1
var dateC=DateComponents()
dateC.hour=hour
dateC.minute=minute
dateC.second=second
dateC.month=month
dateC.day=day
let trigger = UNCalendarNotificationTrigger(dateMatching: dateC, repeats: false)
let requst = UNNotificationRequest(
identifier: UUID().uuidString, content: content,trigger: trigger
)
UNUserNotificationCenter.current().add(requst)
}
func cancelNotification(ids: [String]) {
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: ids)
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ids)
}
Hello I would like to create a local notification manager for my application, my entity is created with a title, a button if the item is finished or not, a date picker and also a boolean if a date is created otherwise nil, to create a notification all is well.
But if for example today it is June 1, 2022, I create an item for June 5, 2022, no worries so far but my come if in the meantime I finish my item, the notification is still active and I don’t understand how the identify UUID in string works…
Here is my code below, but when I call the cancelNotification function in another view I don't understand what to put exactly, item.id, uuid, identifier?....
I'm new to programming, thanks
func pushNotification(input:Date,text:String){
let calendar = Calendar.current
let hour = calendar.component(.hour, from: input)
let minute = calendar.component(.minute, from: input)
let second = calendar.component(.second, from: input)
let day = calendar.component(.day, from: input)
let month = calendar.component(.month, from: input)
let content = UNMutableNotificationContent()
content.title = "Organized"
content.subtitle = text
content.sound = .default
content.badge = 1
var dateC=DateComponents()
dateC.hour=hour
dateC.minute=minute
dateC.second=second
dateC.month=month
dateC.day=day
let trigger = UNCalendarNotificationTrigger(dateMatching: dateC, repeats: false)
let requst = UNNotificationRequest(
identifier: UUID().uuidString, content: content,trigger: trigger
)
UNUserNotificationCenter.current().add(requst)
}
func cancelNotification(ids: [String]) {
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: ids)
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ids)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要提供一个静态字符串标识符,以使取消工作;更改行:
当您想取消所有通知时,您可以执行此操作:
如果您有多个通知并想要取消特定的通知,则可以使用此信息:
或简单地:
You need to give a static string identifier for the cancel to work; change the lines:
And when you want to cancel all notifications, you can do this:
If you have multiple notifications and want to cancel a specific notification, you can use this:
or simply: