Swift 3 - 特定日期的本地通知

时间:2017-06-19 14:11:04

标签: ios swift date calendar uilocalnotification

我需要你帮助一个项目。
我有3个变量,一个用于当天,另一个用于月份,最后一个用于一年。像那样:

var year = 2017 var month = 06 var day = 19

我想发送通知,即使应用程序在我们处于这些变量的日期时已关闭,但我对日历和日期并不是很好。我刚刚制作了这个应用程序。

let myNotification = Notification.Name(rawValue:"MyNotification")

override func viewDidLoad() {
    super.viewDidLoad()

    let nc = NotificationCenter.default
    nc.addObserver(forName:myNotification, object:nil, queue:nil, using:catchNotification)
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    let nc = NotificationCenter.default
    nc.post(name:myNotification,
            object: nil,
            userInfo:["message":"Hello there!", "date":Date()])
}

func catchNotification(notification:Notification) -> Void {
    print("Catch notification")

    guard let userInfo = notification.userInfo,
        let message  = userInfo["message"] as? String,
        let date     = userInfo["date"]    as? Date else {
            print("No userInfo found in notification")
            return
    }

    let alert = UIAlertController(title: "Notification!",
                                  message:"\(message) received at \(date)",
        preferredStyle: UIAlertControllerStyle.alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
    self.present(alert, animated: true, completion: nil)
}


提前谢谢

3 个答案:

答案 0 :(得分:1)

您需要设置本地通知并使用UNCalendarNotificationTrigger在特定日期触发。

let dateComponents = DateComponents(year: year, month: month, day: day)
let yourFireDate = Calendar.current.date(from: dateComponents)
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey:
            "Your notification title", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "Your notification body", arguments: nil)
content.categoryIdentifier = "Your notification category"
content.sound = UNNotificationSound.default()
content.badge = 1

let dateComponents = Calendar.current.dateComponents(Set(arrayLiteral: Calendar.Component.year, Calendar.Component.month, Calendar.Component.day), from: yourFireDate)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
let request = UNNotificationRequest(identifier: "Your notification identifier", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
        if let error = error {
            //handle error
        } else {
            //notification set up successfully
        }
}

答案 1 :(得分:0)

以下是将自定义事件添加到日历的方法。

osm2pgsql -c -d insights_poi -U insights_poi -H beta.cyrrprcqeykj.us-east-1.rds.amazonaws.com -r .pbf -S default.style -W austin_texas.osm.pbf

答案 2 :(得分:0)

swift 3-4 中使用此功能。

  func scheduleLocal() {

        let dateComponents = DateComponents(year: 2019, month: 1, day: 17, hour: 10, minute: 20)
        let yourFireDate = Calendar.current.date(from: dateComponents)

        let notification = UILocalNotification()
        notification.fireDate = yourFireDate
        notification.alertBody = "Hey you! Yeah you! Swipe to unlock!"
        notification.alertAction = "be awesome!"
        notification.soundName = UILocalNotificationDefaultSoundName
        notification.userInfo = ["CustomField1": "w00t"]
        UIApplication.shared.scheduleLocalNotification(notification)


    }
相关问题