Swift中的单个项目的本地通知添加/删除

发布于 2025-02-10 14:05:24 字数 1451 浏览 0 评论 0原文

您好,我想为我的应用程序创建本地通知管理器,我的实体是用标题创建的通知一切都很好。 但是,例如今天是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

再可℃爱ぅ一点好了 2025-02-17 14:05:24

您需要提供一个静态字符串标识符,以使取消工作;更改行:

let requst = UNNotificationRequest(
    identifier: "NotificationIdentifier", content: content,trigger: trigger
)

当您想取消所有通知时,您可以执行此操作:

UNUserNotificationCenter.current().removeAllPendingNotificationRequests()

如果您有多个通知并想要取消特定的通知,则可以使用此信息:

UNUserNotificationCenter.current().getPendingNotificationRequests { (notificationRequests) in
   var identifiers: [String] = []
   for notification:UNNotificationRequest in notificationRequests {
       if notification.identifier == "NotificationIdentifier" {
          identifiers.append(notification.identifier)
       }
   }
   UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: identifiers)
}

或简单地:

UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: "NotificationIdentifier")

You need to give a static string identifier for the cancel to work; change the lines:

let requst = UNNotificationRequest(
    identifier: "NotificationIdentifier", content: content,trigger: trigger
)

And when you want to cancel all notifications, you can do this:

UNUserNotificationCenter.current().removeAllPendingNotificationRequests()

If you have multiple notifications and want to cancel a specific notification, you can use this:

UNUserNotificationCenter.current().getPendingNotificationRequests { (notificationRequests) in
   var identifiers: [String] = []
   for notification:UNNotificationRequest in notificationRequests {
       if notification.identifier == "NotificationIdentifier" {
          identifiers.append(notification.identifier)
       }
   }
   UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: identifiers)
}

or simply:

UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: "NotificationIdentifier")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文