当我单击按钮时,它应该每隔90秒从我的数组发送一个randomElement。但是它发送的总是一样。例如,第一个通知是“ A”,所有其他通知也是“ A”。但是我希望它们从数组中是随机的,我也想将实际的randomElement保存在另一个变量中。
class ViewController: UIViewController {
var meinArray = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "A", "B", "C", "D"]
override func viewDidLoad() {
super.viewDidLoad()
authorize()
}
@IBAction func button_Tapped(_ sender: UIButton) {
if let random = meinArray.randomElement(){
addNotification(time: 90, body: random)
}
}
let uncenter = UNUserNotificationCenter.current()
func authorize(){
uncenter.requestAuthorization(options: [.alert, .sound, .badge]) { (didAllow, error) in
print(error ?? "No error")
}
configure()
}
func configure(){
uncenter.delegate = self
}
func addNotification(time: TimeInterval, 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:UIViewController {
var arrayMein = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
let unCenter = UNUserNotificationCenter.current()
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) {
Timer.scheduledTimer(timeInterval: 90, target: self, selector: #selector(showArray), userInfo: nil, repeats: true)
}
@objc func showArray() {
if let random = arrayMein.randomElement() {
let content = UNMutableNotificationContent()
content.body = random
content.sound = .default
let request = UNNotificationRequest(identifier: "request", content: content, trigger: nil)
unCenter.add(request, withCompletionHandler: nil)
}
}
}
扩展名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")
}
}
尝试以下方法:
@IBAction func button_Tapped(_ sender: UIButton) {
Timer.scheduledTimer(timeInterval: 90, target: self, selector: #selector(showArray), userInfo: nil, repeats: true)
}
@objc func showArray() {
if let random = meinArray.randomElement() {
let content = UNMutableNotificationContent()
content.body = random
content.sound = .default
let request = UNNotificationRequest(identifier: "request", content: content, trigger: nil)
uncenter.add(request, withCompletionHandler: nil)
}
}
...
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler( [.alert, .badge, .sound])
}
看起来像我们这样做:
if let random = meinArray.randomElement(){
addNotification(time: 90, body: random)
}
然后添加到NotificationCenter,该回调将不会再次运行.randomElement(),而是仅采用第一个的元素,并且仅对其进行调用。