2016-03-10 36 views
1

我正在處理應用程序的本地通知,該應用程序應僅在特定日期觸發並在當天重複,直到用戶決定刪除它。每個星期三上午11點都會有一個示例通知。製作只在特定日期重複的本地通知

當用戶按下時間選擇器時,會觸發function - scheduleLocalNotification。

enter image description here

enter image description here

var sundayNotification : UILocalNotification! 
    var mondayNotification : UILocalNotification! 
    var tuesdayNotification : UILocalNotification! 
    var wednesdayNotification : UILocalNotification! 
    var thursdayNotification : UILocalNotification! 
    var fridayNotification : UILocalNotification! 
    var saturdayNotification : UILocalNotification! 


func scheduleLocalNotification() { 

     //Check the Dayname and match fireDate 
     if dayName == "Sunday" { 

      sundayNotification = UILocalNotification() 

      //timeSelected comes from the timePicker i.e. timeSelected = timePicker.date 
      sundayNotification.fireDate = fixNotificationDate(timeSelected) 
      sundayNotification.alertTitle = "Sunday's Workout" 
      sundayNotification.alertBody = "This is the message from Sunday's workout" 
      sundayNotification.alertAction = "Snooze" 
      sundayNotification.category = "workoutReminderID" 
      let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 
      appDelegate.saveContext() 
      UIApplication.sharedApplication().scheduleLocalNotification(sundayNotification) 


     } else if dayName == "Monday" { 

      mondayNotification = UILocalNotification() 
      mondayNotification.fireDate = fixNotificationDate(timeSelected) 
      mondayNotification.alertTitle = "Monday's Workout" 
      mondayNotification.alertBody = "This is the message from Monday's workout" 
      mondayNotification.alertAction = "Snooze" 
      mondayNotification.category = "workoutReminderID" 
      let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 
      appDelegate.saveContext() 
      UIApplication.sharedApplication().scheduleLocalNotification(mondayNotification) 

     } else if ..... } 

} 


func fixNotificationDate(dateToFix: NSDate) -> NSDate { 


     let dateComponets: NSDateComponents = NSCalendar.currentCalendar().components([NSCalendarUnit.Hour, NSCalendarUnit.Minute], fromDate: dateToFix) 

     dateComponets.second = 0 

     if dayName == "Sunday" { 

      dateComponets.weekday = 1 //n = 7 

     } else if dayName == "Monday" { 

      dateComponets.weekday = 2 

     } else if dayName == "Tuesday" { 

      dateComponets.weekday = 3 

     } else if dayName == "Wednesday" { 

      dateComponets.weekday = 4 

     } else if dayName == "Thursday" { 

      dateComponets.weekday = 5 

     } else if dayName == "Friday" { 

      dateComponets.weekday = 6 

     } else if dayName == "Saturday" { 

      dateComponets.weekday = 7 

     } 


     let fixedDate: NSDate! = NSCalendar.currentCalendar().dateFromComponents(dateComponets) 

     return fixedDate 
    } 

做一天的檢查,以分配通知的權利UILocalNotification之後,這仍然奇怪不起作用如在不同的日子(週日上午10點10分和週一上午10點15分)設置不同的時間,因爲通知不會改變任何事情。通知仍然在我沒有選擇的那一天觸發,即在這兩個時間而不是那些日子。

任何關於我可能做錯或失蹤的線索? 在此先感謝。 :)

+0

時間選擇器是一個NSDatePicker嗎? –

+0

是的,它是@ D.Greg – Gugulethu

+0

它是在其他日期還是隻在今天開火? –

回答

1

經過一番工作,我想出了這個。我們首先將日期更改爲用戶選擇的第一個啓動日期。

func setNotificationDay(today: NSDate, selectedDay: Int) -> NSDate { 
    let daysToAdd: Int! 
    let calendar = NSCalendar.currentCalendar() 
    let weekday = calendar.component(.Weekday, fromDate: today) 

    if weekday > selectedDay { 
     daysToAdd = (7 - weekday) + selectedDay 
    } else { 
     daysToAdd = (selectedDay - weekday) 
    } 

    let newDate = calendar.dateByAddingUnit(.Weekday, value: daysToAdd, toDate: today, options: []) 

    return newDate! //if you don't have a date it'll crash 
} 

您可以使用NSDate()而不是將其作爲參數。我會留給你的。

現在,我們需要設置通知。我沒有對fixNotificationDate()做任何事情,但可以輕鬆添加。這裏的關鍵是設置notification.repeatInterval = .Weekday或不會重複。我還添加了一個字典來設置日期名稱。這比重複代碼要好得多。

它確實創建了第二個電話NSCalendar,但您可能找到一種方法避免在代碼中執行此操作。

func scheduleLocalNotification(date: NSDate) { 
    let dayDic = [1:"Sunday", 
     2:"Monday", 
     3:"Tuesday", 
     4:"Wednesday", 
     5:"Thursday", 
     6:"Friday", 
     7:"Saturday"] 

    let calendar = NSCalendar.currentCalendar() 
    let weekday = calendar.component(.Weekday, fromDate: date) 

    let notification = UILocalNotification() 
    notification.fireDate = date 
    notification.repeatInterval = .Weekday 
    notification.alertTitle = String(format: "%@'s Workout", dayDic[weekday]!) 
    notification.alertBody = String(format: "This is the message from %@'s workout", dayDic[weekday]!) 
    notification.alertAction = "Snooze" 
    notification.category = "workoutReminderID" 
    notification.soundName = UILocalNotificationDefaultSoundName 
    UIApplication.sharedApplication().scheduleLocalNotification(notification) 
} 

而這應該可以解決您的問題。我沒有測試它,因爲它至少需要一週! ;)

希望有幫助!

+0

真棒,感謝使用字典的scheduleLocalNotification()。絕對是一個更好的方法。我已經建立了第一個通知,並被解僱。 ;)我會看到對方在接下來的一週內如何做。非常感謝。 – Gugulethu

+0

沒問題。樂意效勞。 –