want to send notification to the users in all days according to day name for example if today is Wednesday and hour is 4 pm send notification with "Today is Wednesday hello do something" and after that day I mean Thursday when clock is 4pm send notification like "hello its Thursday do your sport" and so on can you help me
public struct NotificationManager {
let notificationCenter = UNUserNotificationCenter.current()
static let sharedInstance = NotificationManager()
private func checkRequestAuthorization(completion: @escaping(Bool) -> Void) -> () {
let options: UNAuthorizationOptions = [.alert, .sound,]
notificationCenter.requestAuthorization(options: options) {
(didAllow, error) in
completion(didAllow)
}
}
func setLocalNotifications() {
checkRequestAuthorization { isAllowed in
if isAllowed {
let credentials = getNotificationsCredentials()
addNotificationRequest(title: Localizables.InformationTitles.notificationTitle, body: Localizables.InformationTexts.notificationBody, notifications: credentials)
}
}
}
private func getNotificationsCredentials() -> [CustomNotification] {
return [ CustomNotification(hours: [8,16], title: ["Smiling Sunday", "Self Sunday"], body: ["What makes you smile?", "Let’s take some time to draw today!"]),
CustomNotification(hours: [16], title: ["Mindful Monday"], body: ["Let’s nn today!"]),
CustomNotification(hours: [16], title: ["Thoughtful Tuesday"], body: ["Come and play!"]),
CustomNotification(hours: [16], title: ["Wishful Wednesday"], body: ["Let’s make a wish today!"]),
CustomNotification(hours: [16], title: ["Thankful Thursday"], body: ["What are you thankful for today?"]),
CustomNotification(hours: [16], title: ["Feeling Friday"], body: ["Let’s explore our feelings together!"]),
CustomNotification(hours: [8,16], title: ["Super Saturday", "Singing Saturday"], body: ["What are you super excited for today?", "Let’s dance and sing together!"]),
]
}
private func addNotificationRequest(title: String, body: String, notifications: [CustomNotification]) {
notificationCenter.removeAllPendingNotificationRequests()
let selectedNotification = notifications[Date().dayNumberOfWeek()!-1]
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.sound = UNNotificationSound.default
let gregorian = Calendar(identifier: Calendar.Identifier.gregorian)
let now = Date()
var components = gregorian.dateComponents(in: .autoupdatingCurrent, from: now)
let hours = [14,18]
for hour in hours {
components.timeZone = TimeZone.current
components.hour = hour
components.minute = 28
components.second = 00
let date = gregorian.date(from: components);
let formatter = DateFormatter();
formatter.dateFormat = "MM-dd-yyyy HH:mm";
let dailyTrigger = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: date!);
let trigger = UNCalendarNotificationTrigger(dateMatching: dailyTrigger, repeats: true);
let identifier = UUID().uuidString
let reguest = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
notificationCenter.add(reguest) { err in
print("notification fired")
}
}
}
}
i found it i just created all my notification and with for loop i request for all of them and datetimertriger did the job