【发布时间】:2021-07-14 21:50:12
【问题描述】:
我有一个应用程序,用户需要每天完成一项任务。如果用户没有完成某项任务,他/她会在第二天早上 8 点收到一条提醒,其中包含提示完成任务的短语。
我们想每天早上发送一个短语,但我们不希望它每天都是同一个短语。
这就是我们现在所拥有的:
static func scheduleDailyUnwatchedNotification() {
let notificationMessages = ["Phrase one", "Phrase two", "Phrase 3", "Phrase 4", "Phrase 5"]
let totalMessages = notificationMessages.count
let randomIndex = Int.random(in: 0..<totalMessages)
let center = UNUserNotificationCenter.current()
center.removePendingNotificationRequests(withIdentifiers: ["dailyReminder"])
let content = UNMutableNotificationContent()
content.title = "Reminder"
content.body = notificationMessages[randomIndex]
content.sound = .default
var dateComponents = DateComponents()
dateComponents.hour = 8
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: "dailyReminder", content: content, trigger: trigger)
center.add(request)
}
问题在于,即使选择了一个随机短语,通知也会始终以相同的随机短语重复。
我们怎样才能让它用不同的短语重复?
【问题讨论】:
-
您需要为每一天安排单独的通知,而不是一个重复的通知
标签: ios swift notifications uilocalnotification unnotificationrequest