2016-04-12 7 views
2

Ich versuche, die UILocalNotifications zu planen. Die Funktionalität, die ich implementieren möchte, ist, dass die Benachrichtigung an einem bestimmten Tag, wie (jeden Montag), der vom Benutzer ausgewählt wird, zu wiederholen. Gibt es eine Möglichkeit, die UILocalNotification zu planen, sodass die Benachrichtigung nur an diesem Tag ausgelöst wird?Planen Sie die UILocalNotifiation am selben Tag swift

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

Ja, erstellen Sie zuerst Ihre Schaltflächen, um den Tag auszuwählen, an dem LocalNotification ausgelöst werden soll. – Khuong

Antwort

1

Machen Sie ein wöchentliches es, indem Sie:

notification.repeatInterval = .Weekday // Or maybe .WeekOfYear 

Was auch immer Tag der Woche dateToFire wird, wird die Benachrichtigung, sobald an diesem Tag der Woche in der Woche wiederholt werden.

1

Ich bin nicht sicher, dass dies Ihnen helfen wird, aber ich habe damit schon einmal gearbeitet. Ich erstelle 7 Buttons, um 7 Tage in der Woche auszuwählen. Das ist mein alter Code:

class RemindNotification { 

    var timerNotification = NSTimer() 

    func notification(story: Story) { 
    let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian) 
    let date = NSDate() 

    let dateComponents = calendar!.components([NSCalendarUnit.Day, NSCalendarUnit.WeekOfMonth, NSCalendarUnit.Month,NSCalendarUnit.Year,NSCalendarUnit.Hour,NSCalendarUnit.Minute], fromDate:date) 
    dateComponents.hour = story.remindeAtHour 
    dateComponents.minute = story.remindeAtMinute 
    for index in 0..<story.remindeAtDaysOfWeek.count { 
     if story.remindeAtDaysOfWeek[index] == true { 
     if index != 6{ 
      dateComponents.weekday = index + 2 
      fireNotification(calendar!, dateComponents: dateComponents) 
     } else { 
      dateComponents.weekday = 1 
      fireNotification(calendar!, dateComponents: dateComponents) 
     } 
     } else { 
     dateComponents.weekday = 0 
     } 
    } 
    } 

    func fireNotification(calendar: NSCalendar, dateComponents: NSDateComponents) { 
    let notification = UILocalNotification() 
    notification.alertAction = "Title" 
    notification.alertBody = "It's time to take a photo" 
    notification.repeatInterval = NSCalendarUnit.WeekOfYear 
    notification.fireDate = calendar.dateFromComponents(dateComponents) 
    UIApplication.sharedApplication().scheduleLocalNotification(notification) 
    } 
} 

Das für meine App ist, kann ich den Tag, wählen und dann kann es LocalNotification zu der spezifischen Zeit abzufeuern, die ich in jedem Tag.

Sie können sich darauf beziehen. Ich hoffe das hilft.

Verwandte Themen