swift - 如何在 Swift 中取消本地通知触发器
问题描述
我有一个触发器向用户显示通知:
let content = UNMutableNotificationContent()
content.title = "Title"
content.body = "Body"
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 20, repeats: false)
let request = UNNotificationRequest(identifier: "TestIdentifier", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
有没有办法让我在设置触发器后取消它?
如何在timeInterval
用完之前取消通知并调用通知?
解决方案
您可以通过以下方式取消或删除通知:
let center = UNUserNotificationCenter.current()
删除具有给定标识符的待处理通知
center.removePendingNotificationRequests(withIdentifiers: [“givenIdentifier”])
并删除具有给定标识符的已传递通知
center.removeDeliveredNotifications(withIdentifiers: [“givenIdentifier”])
推荐阅读
- reactjs - 反应总是在状态变化时创建一个新对象吗?
- c++ - 使用 Pybind11 将 Eigen::Tensor 暴露给 Python
- java - java.lang.NoSuchMethodError: 'void org.thymeleaf.standard.processor.AbstractStandardExpressionAttributeTagProcessor
- javascript - 未发送请求时将数据插入数据库
- django - 如何从覆盖范围中排除 django 库
- java - 在java中从一种类型转换为另一种类型的简单方法(当字段相似时)?
- amazon-web-services - 如何通过 N-1 次成功部署绕过 CodeDeploy
- flutter - 如何强制 Container 在 Flutter 中拥有其孩子的高度?
- express - TypeORM 和 Express 中间件
- firebase - E/StorageUtil(5389):获取令牌时出错 java.util.concurrent.ExecutionException:com.google.firebase.internal.api.FirebaseNoSignedInUserException