2016-07-25 76 views
0

enter image description here我正在使用cvcalendar,每天有不同的時間爲fajer,dohor,aser,maghreb,ishaa。例如我選擇了Adan for Fajer,所以我想在日常生活中每天都有adan,每天都有不同的時間。所以當我在DidreceivedLocalNotification中收到通知時,我希望在日曆中的第二天獲得第二天的時間,知道是從CoreData獲取時間。如何根據CVcalendar中的特定時間進行每日本地通知?

在viewWillAppear中

 let date = NSDate() 
     let dateFormatter = NSDateFormatter() 

     dateFormatter.dateFormat = "dd-MM-yyyy" 
     let calendar = NSCalendar.currentCalendar() 
     let calendarForDate = NSCalendar.currentCalendar() 
     let componentsForDate = calendar.components([.Day , .Month , .Year], fromDate: date) 

     let year = componentsForDate.year 
     let month = componentsForDate.month 
     let day = componentsForDate.day 
     ////////// 
     //// Conditions after selecting the user (vibration, beep, Adan) these conditions are testing the selected choice to send a notification to the user on his choice 
     // 



     if prayerCommingFromAdan.id == 0 && prayerCommingFromAdan.ringToneId != 0{ 

      notificationId.id = 0 
      let hours = prayer0.time[0...1] 
      let minutes = prayer0.time[3...4] 
      let fajerTime = "\(month)-\(day)-\(year) \(hours):\(minutes)" 
      var dateFormatter = NSDateFormatter() 
      dateFormatter.dateFormat = "MM-dd-yyyy hh:mm" 
      dateFormatter.timeZone = NSTimeZone.localTimeZone() 

      // convert string into date 
      let dateValue = dateFormatter.dateFromString(fajerTime) as NSDate! 





      var dateComparisionResult:NSComparisonResult = NSDate().compare(dateValue) 

      if dateComparisionResult == NSComparisonResult.OrderedAscending 
      { 
       addNotificationAlarm(year, month: month, day: day, hour: prayer0.time[0...1], minutes: prayer0.time[3...4], soundId: prayerCommingFromAdan.ringToneId, notificationBody: "It is al fajr adan") 

      } 

我應該在DidreceivedLocalNotification做什麼的AppDelegate

回答

0

您可以根據時間使用此代碼來安排每日通知。

func ScheduleMorning() { 
     let calendar: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)! 
     var dateFire=NSDate() 

     var fireComponents = calendar.components([.Hour, .Minute, .Second], fromDate: dateFire) 


     if (fireComponents.hour >= 7) { 
      dateFire=dateFire.dateByAddingTimeInterval(86400) // Use tomorrow's date 

      fireComponents = calendar.components([.Hour, .Minute, .Second], fromDate: dateFire) 

     } 


     fireComponents.hour = 7 
     fireComponents.minute = 0 
     fireComponents.second = 0 

     // Here is the fire time you can change as per your requirement 
     dateFire = calendar.dateFromComponents(fireComponents)! 

     let localNotification = UILocalNotification() 
     localNotification.fireDate = dateFire // Pass your Date here 
     localNotification.alertBody = "Your Message here." 
     localNotification.userInfo = ["CustomField1": "w00t"] 
     localNotification.repeatInterval = NSCalendarUnit.Day 

     UIApplication.sharedApplication().scheduleLocalNotification(localNotification) } 

用於接收

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) { 
     // Do something serious in a real app. 
     print("Received Local Notification:") 
     print(notification.userInfo) 
    } 
+0

阿南德謝謝你,但我的問題是如何讓時間第二天日曆以及如何在AppDelegate中使用DidreceivedLocalNotification? –

+0

我在答案中添加了接收代碼。請檢查一下。 –

+0

您可以使用NSDate手動設置時間以設置本地通知。 –

相關問題