gpt4 book ai didi

ios - Swift 每 5 天重复一次 LocalNotification

转载 作者:搜寻专家 更新时间:2023-11-01 07:03:32 27 4
gpt4 key购买 nike

如何每 5 天上午 10:00 重复一次 LocalNotification

我试过了,还是不行

let content = UNMutableNotificationContent()
content.title = "Hello!"
content.body = "Hello_message_body"
content.sound = UNNotificationSound.default()

let futureTime = Date().addingTimeInterval(5 * 24 * 60 * 60)
var calendar = NSCalendar.current
calendar.timeZone = NSTimeZone.system
var components = calendar.dateComponents([.hour, .minute, .second], from: futureTime)
components.hour = 10
components.minute = 0
components.second = 0

let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
let request = UNNotificationRequest(identifier: "FiveDays", content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request)

最佳答案

您可以使用 UNCalendarNotificationTrigger 获取本地通知,该通知在任何特定时间重复任何定义的日期或星期。

let notificationContent = UNMutableNotificationContent()
notificationContent.title = "Hello!"
//notificationContent.subtitle = "Something"
notificationContent.body = "Hello_message_body"
notificationContent.sound = UNNotificationSound.default

// Add notification for Friday (after 5 days) at 10:00 AM
var dateComponents = DateComponents()
dateComponents.weekday = 5
dateComponents.hour = 10
dateComponents.minute = 0

let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: "notification1", content: notificationContent, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

谢谢。

关于ios - Swift 每 5 天重复一次 LocalNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49607506/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com