I have multiple Core Data entities, one for each day of the week, and each time I add a new Item inside one of these I want to trigger a Local Notification for that specific Item. Since I can add the same Item in more than one entity, how can I set a unique Identifier for each one of them?
I was using the title of that item as an Identifier because they're all different but if I add the same title in two entities then it will display a unique notification of the last item added because it will replace the old one and what I want is to have different notifications with the same item in different entities.
This is my actual code of the Local Notification
let content = UNMutableNotificationContent()
content.title = self.items.itemsData[item].title
content.body = "Notification"
content.sound = UNNotificationSound.default
var dateComponents = DateComponents()
dateComponents.weekday = 2
dateComponents.hour = 8
dateComponents.minute = 00
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: self.items.itemsData[item].title, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
Instead of using self.items.itemsData[item].title
which is the same every time a new UNNotificationRequest
is created for a particular item
use UUID().uuidString
which creates a new unique identifier each time it is called. You could probably use this unique identifier in combination with the title
if that is more accurate for your scenario.
let request = UNNotificationRequest(identifier: self.items.itemsData[item].title + UUID().uuidString, content: content, trigger: trigger)