I can create daily notifications but not weekly ones using swift 3 ios 10 User Notifications framework. Daily works fine but when I add in the .weekday parameter it doesn't send me a notification.
The code is:
for i in 1 ... 7
{
let center = UNUserNotificationCenter.current();
let content = UNMutableNotificationContent();
content.title = "Title";
content.body = "content";
content.sound = UNNotificationSound.default();
var dateComponents = DateComponents();
dateComponents.weekday = i;
// hours is 00 to 11 in string format
if (daynight == "am")
{
dateComponents.hour = Int(hours);
}
else
{
dateComponents.hour = Int(hours)! + 12;
}
// mins is 00 to 59 in string format
dateComponents.minute = Int(mins);
dateComponents.second = 0;
let date = Calendar.current.date(from: dateComponents);
// tiriggerDaily works without weekday for daily but adding weekday doesn't make it weekly
let triggerDaily = Calendar.current.dateComponents([.weekday, .hour, .minute, .second], from: date!);
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true);
let identifier = // a uuid;
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger);
center.add(request, withCompletionHandler:
{
(error) in
if let error = error
{
// Error happened
print("Error: ");
print(error);
}
});
Need to get code to figure out weekly. Used this as guidance: https://useyourloaf.com/blog/local-notifications-with-ios-10/
An example of what I want is this:
A user selects Monday, 11:25pm and it is a Monday at 11:23pm.
Then the user should get a notification in two minutes plus one the following week and so on.
Some other notes/questions:
1. Do you have to wait a week for weekly notifications to start? Like in the example above will it start the following week?
Just a thought since I can't get this working right yet.