How To Use UILocalNotification In Swift

前端 未结 6 1936
温柔的废话
温柔的废话 2021-02-06 01:59

I am trying to figure out how to setup a UILocalNotification in swift but I am not having a lot of luck. I am trying this:

var notification = UILocalNotification         


        
6条回答
  •  无人及你
    2021-02-06 02:23

    func setupNotificationReminder() {
        var title:String = "Your reminder text goes here"
    
        let calendar = NSCalendar.currentCalendar()
        let calendarComponents = NSDateComponents()
        calendarComponents.hour = 7
        calendarComponents.second = 0
        calendarComponents.minute = 0
        calendar.timeZone = NSTimeZone.defaultTimeZone()
        var dateToFire = calendar.dateFromComponents(calendarComponents)
    
        // create a corresponding local notification
        let notification = UILocalNotification()
    
        let dict:NSDictionary = ["ID" : "your ID goes here"]
        notification.userInfo = dict as! [String : String]
        notification.alertBody = "\(title)"
        notification.alertAction = "Open"
        notification.fireDate = dateToFire
        notification.repeatInterval = .Day  // Can be used to repeat the notification
        notification.soundName = UILocalNotificationDefaultSoundName
        UIApplication.sharedApplication().scheduleLocalNotification(notification)
    }
    

提交回复
热议问题