如何设置 UserNotifications Framework 的通知 [英] How I can set a notification which UserNotifications Framework
本文介绍了如何设置 UserNotifications Framework 的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以在一个时间间隔内设置一个通知,但我不知道如何在特定的时间和日期设置它,我尝试了这个但不起作用
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
中将参数repeas
设置为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 Framework 的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文