-1

I have issues with Local Notification, i have UIDatePicker for setup time for notification, and i have ViewController for choose day in which notification will repeat. If i chose repeat every Friday, it's will not work, or another day. But for every day notification is working good.

My code:

func createRequestNotifications() {
    let content = UNMutableNotificationContent()
    content.title = "Time to learn"
    content.body = "Hey bro time to get some knolage)"
    content.sound = UNNotificationSound.default()

    let gregorian = Calendar(identifier: .gregorian)
    let now = Date()
    var components = gregorian.dateComponents([.year, .month, .day, .hour, .minute, .second], from: now)
    print(notification.day)
    components.hour = notifTime
    components.minute = notifMin
    components.second = 0
    components.day = notification.day
    components.weekdayOrdinal = 10
    components.timeZone = .current

    let date = gregorian.date(from: components)!

    if notification.day == 0 {
        let triggerDaily = Calendar.current.dateComponents([.hour,.minute,.second,], from: date)
        let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
        let request = UNNotificationRequest(identifier: "any", content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
        print("evry day")
    } else {
        let triggerDaily = Calendar.current.dateComponents([.hour,.minute,.second, .day,], from: date)
        let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
        let request = UNNotificationRequest(identifier: "any", content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
        print("date setup")
    }
}

notification.day

Is Int value which setup from another ViewController, in which i choose day repeating.

And i tired, because i have no idea where is problem, for every date notification is working, but for special day no :(

Dilip Tiwari
  • 1,441
  • 18
  • 31
Andrew
  • 372
  • 2
  • 5
  • 25

1 Answers1

0

For Particular Day you can set notification Please refer bellow Link https://stackoverflow.com/a/46094544/5069429 according to that you have to separately set the notification for particular day you can use function like this

func setNotificationFor(weekday:Int,hour:Int,minute:Int,title:String,subTitle:String,body:String){
    let notification = UNMutableNotificationContent()
    notification.title = title
    notification.subtitle = subTitle
    notification.body = body


    var dateComponents = DateComponents()
    dateComponents.weekday = weekday  // 0,1,2,3,4,5,6
    dateComponents.hour = hour
    dateComponents.minute = minute
    let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)

    let request = UNNotificationRequest(identifier: "notification1", content: notification, trigger: notificationTrigger)
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
pradip rathod
  • 348
  • 3
  • 20