Schedule local notification swift4

安稳与你 提交于 2019-12-14 04:05:56

问题


I've made a local notification with swift4 every day every at 17.00. and I want the notification to not show up on holidays (Saturday, Sunday). How can I do that? Please help me. It's my coding:

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

回答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)")
        }
    }
}



回答2:


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


来源:https://stackoverflow.com/questions/48981215/schedule-local-notification-swift4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!