Swift 3 - 如何在特定日期设置本地通知

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


Thank you in advance

2个回答

11

您需要设置本地通知,并使用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
        }
}

“yourFireDate”的值是多少? - Rombond
这是一个Date对象,您可以使用年、月和日变量进行初始化。 - Dávid Pásztor
所以我设置了 let yourFireDate = year + month + day - Rombond
什么都没有发生:/ 我认为这是因为日期的原因,我将把日期设置为明天。 - Rombond
1
我已经加了一个小时和一分钟,它可以工作了,谢谢 ^^ - Rombond
显示剩余7条评论

1
在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)


    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接