我想向用户发送时间间隔通知,但它应该在每个通知中发送一个来自我的数组的其他元素(其他消息),但只有在第一个消息中,它的一个随机元素,然后在其他的有所有相同的第一个,但我想每次在通知中的其他消息。
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")
}
}
如果你每次都需要一个随机元素,你必须在一个时间间隔内注册不同的通知,这个时间间隔随着你注册每个通知而不断增加。此外,确保你设置的 repeats
参数设置为false。在这里,你只是做了一个单一的通知,并将其设置为重复,这意味着你会在指定的时间间隔收到相同的通知。下面是一个如何实现这个功能的代码片段。
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)
}
代码现在看起来是这样的。但我没有收到通知。
class ViewController: 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)
}
}
扩展 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")
}
}