如何在swift中设置每周本地通知

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在swift中设置每周本地通知相关的知识,希望对你有一定的参考价值。

我的代码有问题。

我想在xcode7中设置一个本地通知,我正在开发一个日历,你可以把你的大学课程,我想从json数据库获取时间表,我想在课程开始前15分钟通知,但我不知道为什么我的代码不起作用。

这是我想在每周一13:40重复通知的示例。

我可以只设定日期和时间吗?还是我应该指定月份和年份?

  var dateComp:NSDateComponents = NSDateComponents()

    dateComp.day = 01;
    dateComp.hour = 13;
    dateComp.minute = 40;
    dateComp.timeZone = NSTimeZone.systemTimeZone()

    var calender:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    var date:NSDate = calender.dateFromComponents(dateComp)!

    let notification = UILocalNotification()
    notification.fireDate = date
    notification.alertBody = "Swipe to unlock"
    notification.alertAction = "You've got a class soon!"
    notification.soundName = UILocalNotificationDefaultSoundName
    notification.userInfo = ["CustomField1": "w00t"]
    notification.repeatInterval = NSCalendarUnit.WeekOfYear

    UIApplication.sharedApplication().scheduleLocalNotification(notification)
答案

请检查此功能

func setLNotification(weekDay:Int , hour:Int, min:Int, second:Int, alertBody:String, type:String, isRepeate:Bool){
    let calender = NSCalendar(identifier: NSCalendarIdentifierGregorian)
    let dateComp: NSDateComponents?
    let components: NSDateComponents = NSDateComponents()

    if weekDay > 0{
        components.setValue(-50, forComponent: NSCalendarUnit.Year)

        let previousDate = NSCalendar.currentCalendar().dateByAddingComponents(components, toDate: NSDate(), options: NSCalendarOptions(rawValue: 0))!
        dateComp = calender?.components([.Year,.WeekOfMonth,.Month], fromDate: previousDate)
        dateComp?.hour = hour
        dateComp?.minute = min
        dateComp?.second = second
        dateComp?.weekday = weekDay
    }else{
        components.setValue(hour, forComponent: NSCalendarUnit.Hour)
        components.setValue(min, forComponent: NSCalendarUnit.Minute)
        components.setValue(second, forComponent: NSCalendarUnit.Second)
        let notifiDate = NSCalendar.currentCalendar().dateByAddingComponents(components, toDate: NSDate(), options: NSCalendarOptions(rawValue: 0))!
        dateComp = calender?.components([.Year,.Month,.Day,.Hour,.Minute,.Second], fromDate: notifiDate)
    }

    let notification = UILocalNotification()
    if isRepeate == true{
        notification.repeatInterval = NSCalendarUnit.WeekOfYear
        notification.repeatCalendar = calender
    }
    notification.fireDate = calender?.dateFromComponents(dateComp!)
    notification.alertBody = alertBody
    notification.userInfo = ["day":"(weekDay)","type":"(type)"]

    UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
另一答案

** swift 4的每周本地通知

let content = UNMutableNotificationContent()
    content.title = "LocalNotification"
    content.subtitle = "notify"
    content.body = "I am Text"
    content.categoryIdentifier = "alarm"
    content.badge = 1
    content.sound = UNNotificationSound.default()

//配置定期日期。

    var dateComponents = DateComponents()
    dateComponents.calendar = Calendar.current

    dateComponents.weekday = 3
    dateComponents.hour = 13
    dateComponents.minute = 10

    // Create the trigger as a repeating event.
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)

    // Create the request
    let uuidString = UUID().uuidString
    let request = UNNotificationRequest(identifier: uuidString,
                                        content: content, trigger: trigger)

    // Schedule the request with the system.
    let notificationCenter = UNUserNotificationCenter.current()
    notificationCenter.add(request) { (error) in
        if error != nil {
            // Handle any errors.
            print("************Error***************")
        }
    }
}

以上是关于如何在swift中设置每周本地通知的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Unity(C#,目前针对 iOS)中设置每周重复推送通知?

如何在 Swift 中设置推送通知

使用本地通知 api 在 swift 3 中触发每周通知

在 Swift 中设置多次通知

每周特定日期的本地通知

在本地通知中设置repeatInterval