首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计划本地通知swift4

计划本地通知swift4
EN

Stack Overflow用户
提问于 2018-02-26 11:54:28
回答 2查看 2.4K关注 0票数 1

我每天下午17:00都会向swift4发出本地通知。我希望通知不会在节假日(周六、周日)出现。我该怎么做呢?

下面是我的代码:

代码语言:javascript
复制
// schedule notification every day
        var dateComponents = DateComponents ()
        dateComponents.hour = 17
        dateComponents.minute = 00
        dateComponents.day = 7
        let trigger = UNCalendarNotificationTrigger (dateMatching: dateComponents, repeats: true)
        let request = UNNotificationRequest.init (identifier: "Everyday", content: content, trigger: trigger)
EN

回答 2

Stack Overflow用户

发布于 2018-03-09 16:11:43

代码语言:javascript
复制
func createDate(weekday: Int, hour: Int, minute: Int, year: Int)->Date{

    var components = DateComponents()
    components.hour = hour
    components.minute = minute
    components.year = year
    components.weekday = weekday // sunday = 1 ... saturday = 7
    components.weekdayOrdinal = 10
    components.timeZone = .current

    let calendar = Calendar(identifier: .gregorian)
    return calendar.date(from: components)!
}

//Schedule Notification with weekly bases.
func scheduleNotification(at date: Date, body: String, titles:String) {

    let triggerWeekly = Calendar.current.dateComponents([.weekday,.hour,.minute,.second,], from: date)

    let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true)

    let content = UNMutableNotificationContent()
    content.title = titles
    content.body = body
    content.sound = UNNotificationSound.default()
    content.categoryIdentifier = "todoList"

    let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)

    UNUserNotificationCenter.current().delegate = self
    //UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
    UNUserNotificationCenter.current().add(request) {(error) in
        if let error = error {
            print(" We had an error: \(error)")
        }
    }
}
票数 1
EN

Stack Overflow用户

发布于 2018-12-23 17:24:00

Swift 4

代码语言:javascript
复制
?///CreateDateForNotification
    func createDate(day: Int, month : Int, hour: Int, minute: Int, year: Int)->Date{

var components = DateComponents()
components.hour = hour
components.minute = minute
components.year = year
components.day = day
components.month = month

components.timeZone = .current

let calendar = Calendar(identifier: .gregorian)
return calendar.date(from: components)!
}

?///CreateNitification
func scheduleNotification(at date: Date, identifierUnic : String, body: String, titles:String) {

let triggerWeekly = Calendar.current.dateComponents([.day, .month, .hour,.minute, .year], from: date)

let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true)

let content = UNMutableNotificationContent()
content.title = titles
content.body = body
content.sound = UNNotificationSound.default
content.categoryIdentifier = "todoList2"

let request = UNNotificationRequest(identifier: identifierUnic, content: content, trigger: trigger)

UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate

/// UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["textNotification2"])
/// UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print(" We had an error: \(error)")
}}
}

?///Use 
scheduleNotification(at: createDate(day : 11, month : 2, hour: 15, minute: 5, year: 2018), identifierUnic: "unic1", body: "Notification day", titles: "Notification titles1")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48981215

复制
相关文章

相似问题

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