将设置为相同的本地通知限制为一个
Limiting local notifications set at the same to one
我已经实现了一个用于设置发送本地通知的提醒的日期选择器。在测试过程中,我注意到我可以同时设置多次提醒。我假设系统会识别出我已经在那个特定时间设置了一次,并且不会保存另一个提醒。令人惊讶的是,该应用程序同时发送了不止一次通知。
我想我没有正确实现这个,所以我决定测试默认的时钟应用程序。同样,我同时设置了两个闹钟,同时收到了两个通知:
这是通知的正常行为吗?我的假设错了吗?当用户想要将不同的消息分配给单独的通知时,这可能没问题。但是,就我而言,该应用程序应该通过静态消息提醒用户一项简单的任务。
有没有办法解决这个问题"problem"?在我看来,如果我已经设置了提醒,应用程序仍应显示已保存新提醒,但实际上并未保存新提醒。
这是正常的预期行为。两个通知可能有相同的时间但完全不同的数据,所以它们不限于每单位时间一个。
我会设置通知的唯一标识符并在数组中跟踪它们。
这是一个例子:
let center = UNUserNotificationCenter.current()
// Setup content
let content = UNMutableNotificationContent()
content.title = "Notificaion title"
content.body = "Yoir text here"
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "YouCanHaveDifferentCategories"
content.userInfo = ["myKey": "myValue"]
// Setup trigger time
var calendar = Calendar.current
let now = Date()
let date = calendar.date(byAdding: .second, value: 10, to: now)
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: false)
// Create request
// Set any unique ID here. I use the UUID tools. This value is used by notificaion center, but I alsp store it in an array property of the relevant object in my data model, so the notification can be removed if the user deletes that object.
let uniqueID = UUID().uuidString
let request = UNNotificationRequest(identifier: uniqueID, content: content, trigger: trigger)
// Add the notification request
center.add(request)
然后,您可以使用这些方法有选择地删除通知,只要您在数组或标识符的数据模型中有记录即可。您可以传递一个数组来一次删除多个。
center.removePendingNotificationRequests(withIdentifiers: "UniqueID")
center.removeDeliveredNotifications(withIdentifiers: "UniqueID")
我已经实现了一个用于设置发送本地通知的提醒的日期选择器。在测试过程中,我注意到我可以同时设置多次提醒。我假设系统会识别出我已经在那个特定时间设置了一次,并且不会保存另一个提醒。令人惊讶的是,该应用程序同时发送了不止一次通知。
我想我没有正确实现这个,所以我决定测试默认的时钟应用程序。同样,我同时设置了两个闹钟,同时收到了两个通知:
这是通知的正常行为吗?我的假设错了吗?当用户想要将不同的消息分配给单独的通知时,这可能没问题。但是,就我而言,该应用程序应该通过静态消息提醒用户一项简单的任务。
有没有办法解决这个问题"problem"?在我看来,如果我已经设置了提醒,应用程序仍应显示已保存新提醒,但实际上并未保存新提醒。
这是正常的预期行为。两个通知可能有相同的时间但完全不同的数据,所以它们不限于每单位时间一个。
我会设置通知的唯一标识符并在数组中跟踪它们。
这是一个例子:
let center = UNUserNotificationCenter.current()
// Setup content
let content = UNMutableNotificationContent()
content.title = "Notificaion title"
content.body = "Yoir text here"
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "YouCanHaveDifferentCategories"
content.userInfo = ["myKey": "myValue"]
// Setup trigger time
var calendar = Calendar.current
let now = Date()
let date = calendar.date(byAdding: .second, value: 10, to: now)
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: false)
// Create request
// Set any unique ID here. I use the UUID tools. This value is used by notificaion center, but I alsp store it in an array property of the relevant object in my data model, so the notification can be removed if the user deletes that object.
let uniqueID = UUID().uuidString
let request = UNNotificationRequest(identifier: uniqueID, content: content, trigger: trigger)
// Add the notification request
center.add(request)
然后,您可以使用这些方法有选择地删除通知,只要您在数组或标识符的数据模型中有记录即可。您可以传递一个数组来一次删除多个。
center.removePendingNotificationRequests(withIdentifiers: "UniqueID")
center.removeDeliveredNotifications(withIdentifiers: "UniqueID")