2017-06-19 46 views
1

我需要您的帮助来完成一个项目。
我有3个变量,一个为当天,另一个为本月和最后一年。这样的:Swift 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) 
} 


预先感谢您

回答

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 
     } 
} 
+0

“yourFireDate”的值是什么? – Rombond

+0

它是一个'Date'对象,它使用变量year,month和day来初始化。 –

+0

所以我设置了'let yourFireDate = year + month + day' – Rombond

0

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

let store = EKEventStore() 
store.requestAccessToEntityType(.Event) {(granted, error) in 
    if !granted { return } 
    var event = EKEvent(eventStore: store) 
    event.title = "Event" 
    event.startDate = NSDate() //Event date 
    event.endDate = event.startDate.dateByAddingTimeInterval(60*60) //even duration ex.1hr 
    event.calendar =  store.defaultCalendarForNewEvents 
    do { 
     try store.saveEvent(event, span: .ThisEvent, commit: true) 
     self.savedEventId = event.eventIdentifier //save this id for future reference 
}  catch { 
    // Display error 
    } 
} 
+0

你在哪里看对问题中日历事件的任何引用?问题显然是在特定的日期发送本地通知,而且与设置日历活动无关。 –

+0

他在他的问题中标记了一个压光标签。 –