1

I'm using actionable notifications. Following data coming from backend :

  1. notificationId (String)
  2. notification title, body
  3. Each time different titles for 2 action buttons

So, I'm registering both UNNotificationCategory and categoryIdentifier(for UNMutableNotificationContent) with unique notificationId.

Problem statement :- Definitely, all Incoming notifications pops up with their notification actions. but when I check action buttons of those notifications inside NotificationCenter. Only lastmost notification showing his actions buttons and others showing without buttons.

Following code snippet executes for each incoming notification : -

This is how I register notification actions and notificationType

struct NotificationStruct {
    struct Action {
        static let positive = "positive" 
        static let negative = "negative"
    }
}


//Registering notification actions with notificationCatagory
//This function called each time for incoming notification
func registerCatagory ()
{
    let actionPositive = UNNotificationAction (
        identifier : NotificationStruct.Action.positive,
        title : buttonPositiveLabel,
        options : []
    )
    let actionNegative = UNNotificationAction (
        identifier : NotificationStruct.Action.negative,
        title : buttonNegativeLabel,
        options : []
    )
    let notificationCategory = UNNotificationCategory (
        identifier : notificationId,
        actions : [actionPositive, actionNegative],
        intentIdentifiers : [],
        options : []
    )
    UNUserNotificationCenter.current ()
    .setNotificationCategories ([notificationCategory])
}

This is how I register notification catagoryIdentifier to trigger notification:

   func triggerNotificaion ()
    {
        let content = UNMutableNotificationContent ()
        content.title = title 
        content.body = body 
        content.categoryIdentifier = notificationId 
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 2, repeats: false)
        let request = UNNotificationRequest (
            identifier : String (getRandomId()),
            content : content,
            trigger : trigger
        )
        //scheduling the notification
        UNUserNotificationCenter.current ()
        .add (request, withCompletionHandler: nil)
    }

    func getRandomId () -> Int {
    let randomInt = Int.random (in: 0..<999999)
    return randomInt
    }

Please guys help me!!!

Shivam Sharma
  • 1,015
  • 11
  • 19

0 Answers0