2017-06-19 55 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

他在他的問題中標記了一個壓光標籤。 –