我如何设置一个通知,哪个UserNotifications框架 [英] How I can set a notification which UserNotifications Framework
本文介绍了我如何设置一个通知,哪个UserNotifications框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以在一个时间间隔内设置通知,但是我不知道如何在特定的时间和日期发出通知,我尝试这样做但不起作用
I can set a notification in a time interval but I don't know how make it in a specific time and date, I try this but don't work
let center = UNUserNotificationCenter.current()
func notificationSender(){
center.requestAuthorization([.sound, .alert]) {
(granted, error) in
// We can register for remote notifications here too!
}
var date = DateComponents()
date.hour = 13
date.minute = 57
let trigger = UNCalendarNotificationTrigger.init(dateMatching: date , repeats: false)
let content = UNNotificationContent()
// edit your content
let notification = UNNotificationRequest(identifier: "myNotification", content: content, trigger: trigger)
center.add(通知) }
center.add(notification) }
该通知需要在每个星期一和星期五的下午3点重复
the notification need to be repeated every Monday and Friday at 3 pm
推荐答案
您快到了.还有很多事情要做.
You're almost there. Not much left for you to do.
您缺少的内容如下:
- 您应在日期部分添加
weekday
,1
是星期天,因此对于您而言,您需要将其设置为2
用于星期一通知,将6
设置为星期五通知 - 如果需要重复此操作,则需要在
UNCalendarNotificationTrigger
中将参数repeats
设置为true
.
- You should add
weekday
to your date components,1
is for Sunday so in your case you will need to set it to2
for the Monday notification and6
for your Friday notification - If you need it to repeat you need to set the parameter
repeats
totrue
in yourUNCalendarNotificationTrigger
.
这里是一个例子.
// Create date components that will match each Monday at 15:00
var dateComponents = DateComponents()
dateComponents.hour = 15
dateComponents.minute = 0
dateComponents.weekday = 2 // Monday
// Create a calendar trigger for our date compontents that will repeat
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents,
repeats: true)
// Create the content for our notification
let content = UNMutableNotificationContent()
content.title = "Foobar"
content.body = "You will see this notification each monday at 15:00"
// Create the actual notification
let request = UNNotificationRequest(identifier: "foo",
content: content,
trigger: trigger)
// Add our notification to the notification center
UNUserNotificationCenter.current().add(request)
这篇关于我如何设置一个通知,哪个UserNotifications框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文