Post

Replies

Boosts

Views

Activity

Notifications in SwiftUI
I have a reminder app that contains a list of items with anniversary dates. The application is written in SwiftUI. I want to know what the best way is to manage notifications on the anniversary dates. If I have 5 items in my list, each with their own different anniversary date, I want to allow the user to select a time for notifications (and day of, or day before notifications). The user can therefore select to be notified at 9 AM on the date of the anniversary. The user will specify the reminder time on the settings page. I will then create notifications for each date in the list to trigger at the time the user selected. My questions are as follows: Must I loop through the list and create a notification using UNUserNotificationCenter for each item recursively? The list might get big, but I don't see a way to create notifications for each item in the list without looping through it and creating a notification for each.... but is this the right way to do it? I was thinking of creating an @ObservableObject that I can populate a @Published property of array for all the anniversary dates and items. I can then pick this up somewhere else in my app when I want to create the notifications for each item. Each notification will be created with identifier: UUID().uuidString. But I will probably have to keep track of the item and the UUID used to create the notification... what if the specific notification needs to be cancelled using the removePendingNotificationRequests(withIdentifiers:) method of UNUserNotificationCenter. To do this, I need to know the UUID of the item to cancel? Lastly, what if one or more anniversary dates change? The created notifications will no longer be valid. How would be the best way to manage this? Perhaps I am looking at the whole situation incorrectly? I'm not sure. I'm a C# developer, writing a SwiftUI app for the first time. Some of the concepts are a bit foreign to me, so forgive me if some of the terminology I use is incorrect.
0
0
742
Jan ’22