icekomo
icekomo

Reputation: 9466

Trying to remove multiple local notifications based on the Identifier

I have an app, that creates users. Each of those users may create an object. I give each user the ability to set a local notification on that object. My naming convention for the identifier for the notification is to use the persons name + the name of the object they created. What I would like to do is delete all notifications when that person is deleted from the App.

I need to somehow loop through all the objects of that person, and use that name to identify the notifications I need to delete.

Here is what I'm trying

        // remove local notifications
        let center = UNUserNotificationCenter.current()
        let personToSerch = person.name!

        var filterdItemsArray = [String]()

        center.getPendingNotificationRequests { (notifications) in

            print("Count: \(notifications.count)")

            func filterContentForSearchText(searchText: String) {
                filterdItemsArray = notifications.filter { item in
                    return item.contains(searchText)
                }
            }

            filterContentForSearchText(searchText: personToSerch)
            print("\(filterdItemsArray.count) count of the filter array")

        }

      center.removePendingNotificationRequests(withIdentifiers: filterdItemsArray)

It just doesn't seem to be working and my return line throws an error that reads: Value of type UNNotificaionRequest has no member contains.

Upvotes: 0

Views: 576

Answers (1)

CST
CST

Reputation: 313

Check if following code works for you.

let center = UNUserNotificationCenter.current()
        let personToSerch = person.name!
        center.getPendingNotificationRequests { (notifications) in
            for item in notifications {
                if(item.identifier.contains(personToSerch)) {
                    center.removePendingNotificationRequests(withIdentifiers: [item.identifier])
                }
            }
        }

Upvotes: 1

Related Questions