3

We have implemented Actionable Notification using Notification Extension Service. We receive push notifications and it shows the proper content for notification title, body, action buttons. We have defined 3 category say onebtn, twobtn, threebtn and all the unique actions for every category.

So, everything works fine for one notification but as we receive more than one notification in notification tray the recent notification action button's text replace to all other notification's action button's text. Ex. I have received notification for category threebtn then it will show me three buttons with three different texts say Now, Later and Cancel(These three button names will be the part of pay load received from push notification). Now I have received one more notification for category twobtn with button names, Reply & Mark as read and shown for that notification.

Now the issue is, it overwrites the button names of category twobtn to the notification which is received for threebtn notification. It means when I drag and open notification of category threebtn, its button names will be Reply, Mark as paid & Cancel (third default text defined while assigning action). So every notification will have name Reply & Mark as Paid.

Tried Solutions:

  • Set categories to plist as dictionary under NSNotification (previously we added categories at runtime)
  • Assigned unique action and category name.

I am stuck with this issue since a week but not finding any proper solution. Kindly provide me some hint so I can fix this.

Thanks.

class NotificationService: UNNotificationServiceExtension {

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {

            let (pushNotificationBean, _) = saveNotificationAppGroupDefault(saveNotificationUserInfo: request.content.userInfo)

            registerNotificationCategory(pushNotificationBean: pushNotificationBean)

            self.contentHandler = contentHandler
            bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

        }

            // MARK: - Register Notificaion Categories and Actions
            func registerNotificationCategory(pushNotificationBean: PushNotificationBean) {
                // Define Actions
                if #available(iOS 10.0, *) {
                    // Define Action Yes

                    var singleBtnAction : UNNotificationAction

                    var trippleBtnYesAction : UNNotificationAction
                    var trippleBtnNoAction : UNNotificationAction
                    var trippleBtnPartialAction : UNNotificationAction

                    var doubleBtnYesAction : UNNotificationAction
                    var doubleBtNoAction : UNNotificationAction

                    // Positve Button Text
                    if let okActionText = pushNotificationBean.actionPositiveText, okActionText.count > 0{
                        // Single Button Action
                        singleBtnAction = UNNotificationAction(identifier: self.OkAction,
                                                            title: okActionText,
                                                            options: .foreground)
                        // Double Button Action
                        doubleBtnYesAction = UNNotificationAction(identifier: self.YesAction,
                                                                  title: okActionText,
                                                                  options: .foreground)
                        // Tripple Button Action
                        trippleBtnYesAction = UNNotificationAction(identifier: self.YesAction,
                                                                   title: okActionText,
                                                                   options: .foreground)
                    }else{
                        // Single Button Action
                        singleBtnAction = UNNotificationAction(identifier: self.OkAction,
                                                            title: self.OkAction,
                                                            options: .foreground)
                        // Double Button Action
                        doubleBtnYesAction = UNNotificationAction(identifier: self.YesAction,
                                                                  title: self.YesAction,
                                                                  options: .foreground)
                        // Tripple Button Action
                        trippleBtnYesAction = UNNotificationAction(identifier: self.YesAction,
                                                                   title: self.YesAction,
                                                                   options: .foreground)
                    }

                    // Negative Action Text
                    if let noActionText = pushNotificationBean.actionNegativeText, noActionText.count > 0{
                        // Double Button Action
                        doubleBtNoAction = UNNotificationAction(identifier: self.NoAction,
                                                                title: noActionText,
                                                                options: .foreground)

                        // Tripple Button Action
                        trippleBtnNoAction = UNNotificationAction(identifier: self.NoAction,
                                                                  title: noActionText,
                                                                  options: .foreground)
                    } else{
                        // Double Button Action
                        doubleBtNoAction = UNNotificationAction(identifier: self.NoAction,
                                                                title: self.NoAction,
                                                                options: .foreground)
                        // Tripple Button Action
                        trippleBtnNoAction = UNNotificationAction(identifier: self.NoAction,
                                                                  title: self.NoAction,
                                                                  options: .foreground)
                    }

                    // Partial Action Text
                    if let trippleBtnPartialActionText = pushNotificationBean.actionNeutralText, trippleBtnPartialActionText.count > 0{
                        // Tripple Button Action
                        trippleBtnPartialAction = UNNotificationAction(identifier: self.PartialAction,
                                                                 title: trippleBtnPartialActionText,
                                                                 options: .foreground)
                    }else{
                        // Tripple Button Action
                        trippleBtnPartialAction = UNNotificationAction(identifier: self.PartialAction,
                                                                 title: self.PartialAction,
                                                                 options: .foreground)
                    }

                    // Define Single Category
                    let singleButtonCategory = UNNotificationCategory(identifier: self.SingleButton,
                                                                      actions: [singleBtnAction],
                                                                      intentIdentifiers: [self.OkAction],
                                                                      options: [])

                    // Define Double Category
                    let doubleButtonCategory = UNNotificationCategory(identifier: self.DoubleButton,
                                                                      actions: [doubleBtnYesAction,doubleBtNoAction],
                                                                      intentIdentifiers: [self.YesAction,self.NoAction],
                                                                      options: [])

                    // Define Tripple Category
                    let tripleButtonCategory = UNNotificationCategory(identifier: self.TripleButton,
                                                                      actions: [trippleBtnYesAction,trippleBtnNoAction,trippleBtnPartialAction],
                                                                      intentIdentifiers: [self.YesAction,self.NoAction,self.PartialAction],
                                                                      options: [])

                    UNUserNotificationCenter.current().setNotificationCategories([singleButtonCategory,doubleButtonCategory,tripleButtonCategory])
                }
            }
        }

First Notification with three buttons

Second Notification with One Button

First Notification with three Buttons after receiving Second Notification

Saurabh
  • 457
  • 2
  • 8
  • 26

0 Answers0