安排本地通知swift4

时间:2018-02-26 03:54:28

标签: notifications swift4 ios11

我每天都在17.00用swift4做一个本地通知。我希望通知不会出现在假期(周六,周日)。我怎样才能做到这一点?请帮我。这是我的编码:

setuptools

2 个答案:

答案 0 :(得分:1)

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 :(得分:0)

雨燕4

///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")