4

I'm just making my first app and I'm facing probably the biggest problem in it.I tried to make repeating local notifications with for loop each having different category but for some reason it doesn't work. I actually get all of the notifications but when I pull down on notification to see the actions I don't see any actions.Have you ever faced this kind of problem and if you do how did you manage to solve it?The notifications I am trying to make are repeating local notifications scheduled for today.

Here is my code:

for index in 0..<workingDays[3].subjects!.count {
    howManyLeft -= 1
    var seperated = workingDays[3].subjects![index].endsAt!.components(separatedBy: ":")
    var dateComponents = DateComponents()
    dateComponents.hour = Int(seperated[0])
    dateComponents.minute = Int(seperated[1])
    dateComponents.weekday = 5

    // make category
    let actionBaby = UNNotificationAction(identifier: "oneaction\(index)", title: "something\(index)", options: .foreground)
    let category = UNNotificationCategory(identifier: "\(index).5", actions: [actionBaby], intentIdentifiers: [], hiddenPreviewsBodyPlaceholder: "idk", options: [])
    UNUserNotificationCenter.current().setNotificationCategories([category])

    // If contition is true make notification
    if index + 1 >= 0 && index + 1 < workingDays[3].subjects!.count {
        let notification = UNMutableNotificationContent()
        notification.categoryIdentifier = "\(index).5"
        notification.title = "someRandom\(index)"
        let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
        let request = UNNotificationRequest(identifier: "\(index)5",
            content: notification,
            trigger: trigger)
        UNUserNotificationCenter.current().add(request)
    }
}
  • Be more specific. " it doesn't work" doesn't mean anything. Do you get a runtime/compile time error? If not, what is the expected behaviour and what is the actual? – Dávid Pásztor Nov 16 '17 at 18:14
  • My bad, sorry on that, I actually get all of the notifications but when I pull down on notification to see the actions I don't see any actions. – Blockchain Developer Nov 16 '17 at 18:19
  • 2
    For what it's worth, a for-loop seems like a very fragile way of approaching this. Consider using a struct or class to hold the notifications and their actions, and using a [forEach](https://developer.apple.com/documentation/swift/array/1689783-foreach) iterator instead. – brandonscript Nov 16 '17 at 18:59
  • I don't get it how can I use struct for example to make this happen? Can you give me an example of how the struct would look like ? I'm beginner – Blockchain Developer Nov 17 '17 at 09:51
  • 1
    It is crazy to keep creating the same category over and over as you are doing. Here's something to try: Do not set the category at the same moment you create the notification request. You ought to have set the category _once_ long before, e.g. in your `applicationDidFinishLaunching`. Categories are longstanding permanent objects. – matt Nov 19 '17 at 17:44
  • Thanks for the help dude, but I don't think I can do that because I need most of the data in this tableViewController when i set up the categories because the actions in the category are based on the data in this tableViewController and I can't access that data in appDelegate file as far as I know. Or can I? Can I somehow set up categories in this tableViewController where I actually make all of the notification ?.I am beginner. – Blockchain Developer Nov 19 '17 at 18:11

0 Answers0