首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何设置UserNotifications框架的通知

如何设置UserNotifications框架的通知
EN

Stack Overflow用户
提问于 2016-07-07 18:05:56
回答 1查看 692关注 0票数 1

我可以在一个时间间隔内设置一个通知,但我不知道如何在特定的时间和日期内发出通知,我尝试了这个,但不工作。

代码语言:javascript
复制
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(通知)}

通知须于每星期一及五下午三时重播。

EN

回答 1

Stack Overflow用户

发布于 2016-10-15 14:39:23

你快到了。你没什么可做的了。

您所缺少的内容如下:

  • 应该将weekday添加到日期组件中,1用于星期日,因此在您的情况下,您需要将其设置为周一通知的2和周五通知的6
  • 如果需要重复,则需要将参数repeats设置为UNCalendarNotificationTrigger中的true

下面是一个例子。

代码语言:javascript
复制
// 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)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38252389

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档