在每个 TimeIntervalNotification 中不重复的随机元素
Posted
技术标签:
【中文标题】在每个 TimeIntervalNotification 中不重复的随机元素【英文标题】:Random Element that is not repeating in every TimeIntervalNotification 【发布时间】:2020-06-06 06:19:28 【问题描述】:我想向用户发送 TimeInterval 通知,但它应该在每个通知中发送我数组中的其他元素(其他消息) 但只有在第一条消息中它是一个随机元素,然后在其他消息中都与第一条相同。 但我希望每次在通知中都有一条其他消息。
class ViewController: UIViewController
let unCenter = UNUserNotificationCenter.current()
var myFirstArray = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
override func viewDidLoad()
super.viewDidLoad()
unCenter.requestAuthorization(options: [.alert, .sound, .badge]) (didAllow, error) in
print(error ?? "No error")
unCenter.delegate = self
@IBAction func button_Tapped(_ sender: UIButton)
if let random = myFirstArray.randomElement()
sendNotification(body: random, time: 70)
func sendNotification(body: String, time: TimeInterval)
let content = UNMutableNotificationContent()
content.body = body
content.sound = .default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: time, repeats: true)
let request = UNNotificationRequest(identifier: "request", content: content, trigger: trigger)
unCenter.add(request, withCompletionHandler: nil)
extension ViewController: UNUserNotificationCenterDelegate
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
print("will present")
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
print("did receive")
【问题讨论】:
【参考方案1】:如果您每次都需要一个随机元素,则您必须以随着您注册每个通知而不断增加的时间间隔注册不同的通知。此外,请确保将 repeats
参数集设置为 false。在这里,您只是发出一个通知并将其设置为重复,这意味着您在指定的时间间隔内收到相同的通知。这是如何实现此目的的代码 sn-p。
var time: Double = 0
@IBAction func button_Tapped(_ sender: UIButton)
for _ in 0..<myFirstArray.count
if let random = myFirstArray.randomElement()
time += 70
sendNotification(body: random)
func sendNotification(body: String)
let content = UNMutableNotificationContent()
content.body = body
content.sound = .default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: time, repeats: true)
let request = UNNotificationRequest(identifier: "request", content: content, trigger: trigger)
unCenter.add(request, withCompletionHandler: nil)
【讨论】:
嘿,它不起作用。我没有收到通知。我用代码回答了我的问题。请看一下:) 这与您刚刚发布的内容有何不同?您只是将整个代码发布在 ViewController 中,此处不需要。在提出问题时,您应该查看 how-to-ask 和 minimal-reproducible-example。【参考方案2】:代码现在看起来是这样:但我没有收到通知。
类视图控制器:UIViewController
var time: Double = 0
let unCenter = UNUserNotificationCenter.current()
var myFirstArray = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
override func viewDidLoad()
super.viewDidLoad()
unCenter.requestAuthorization(options: [.alert, .sound, .badge]) (didAllow, error) in
print(error ?? "No error")
unCenter.delegate = self
@IBAction func button_Tapped(_ sender: UIButton)
for _ in 0..<myFirstArray.count
if let random = myFirstArray.randomElement()
time += 70
sendNotification(body: random)
func sendNotification(body: String)
let content = UNMutableNotificationContent()
content.body = body
content.sound = .default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: time, repeats: false)
let request = UNNotificationRequest(identifier: "request", content: content, trigger: trigger)
unCenter.add(request, withCompletionHandler: nil)
扩展视图控制器:UNUserNotificationCenterDelegate
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
print("will present")
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
print("did receive")
【讨论】:
以上是关于在每个 TimeIntervalNotification 中不重复的随机元素的主要内容,如果未能解决你的问题,请参考以下文章
在每个UITableViewCell单元格中添加键的每个出现
Spring Rest API 拦截器在每个/每个请求上添加响应标头
在 Sublime Text 2 中为每个选择添加一个数字,每个选择增加一次