1

I am working on an iOS app based on device to device push notifications. My notifications are working fine when the app is in foreground and background. But it doesn't work as per requirement, when the app is in killed state. I have a custom notification which has two buttons (Accept and Reject). When the app is in killed state, I am able to get the notification, but cannot perform action on buttons. But when the app is in foreground or background, it works perfectly. Can you guys please help me this? I am stuck in this for long now.

Note: I am using FCM to generate push notifications. And the payload is in index.js file.

This is my function:

func setActionCategories() {
    let acceptAction = UNNotificationAction(
            identifier:NAString().notificationAcceptIdentifier(),
            title:NAString().accept().capitalized,
            options: [.init(rawValue:0)])

    let rejectAction = UNNotificationAction(
            identifier:NAString().notificationRejectIdentifier(),
            title:NAString().reject().capitalized,
            options: [.init(rawValue:0)])

    let actionCategory = UNNotificationCategory(
            identifier:NAString().notificationActionCategory(),
            actions: [acceptAction, rejectAction],
    intentIdentifiers: [],
    options: [.customDismissAction])

    UNUserNotificationCenter.current().setNotificationCategories(
            [actionCategory])
}

This is where I am performing action on buttons:

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo

    let notificationType = userInfo[Constants.FIREBASE_NOTIFICATION_TYPE] as? String

    if response.notification.request.content.categoryIdentifier == NAString().notificationActionCategory() {


        switch response.actionIdentifier {

        case NAString().notificationAcceptIdentifier():
            Print(“Accept”)


        case NAString().notificationRejectIdentifier():
            Print(“Reject”)
        }
    }
    completionHandler()
}
KENdi
  • 7,576
  • 2
  • 16
  • 31
Ashish Jha
  • 173
  • 1
  • 3
  • 18
  • What do you mean by you cannot perform action on buttons? You tap on them but nothing happens? Or app opens but expected action is not triggered? – inokey Sep 12 '18 at 13:12
  • Also please provide some code when you're getting remote notifications. – inokey Sep 12 '18 at 13:17
  • @inokey Yeah. I tap on them and nothing happens. I am expecting an action to happen on the click of Accept and Reject buttons. But its not happening right now. – Ashish Jha Sep 12 '18 at 13:18
  • Please check this answer. I think this might be it: https://stackoverflow.com/questions/50241353/firebase-silent-notification-does-not-start-up-a-closed-ios-app – inokey Sep 12 '18 at 13:23
  • I have added my code – Ashish Jha Sep 12 '18 at 13:27
  • @inokey that's not related. When you tap on a notification then you're **launching the app** – mfaani Sep 12 '18 at 17:40
  • Currenlty you're doing `[.init(rawValue:0)]` which is no different from doing `[]`. But that won't solve anything. As for your issue I'm not sure 1) Are the functions getting called? If so then I believe it has something to do with the notifications options. Maybe try `options: [.foreground]`. – mfaani Sep 12 '18 at 18:00
  • Did you also check to see if tapping from a terminated state your code follows the same line of codes that it goes through when it's tapped from a foreground/background state? Please confirm this. The major difference when tapping from terminated state is just that you're **launching** the device ie you're hitting your `didFinishLaunching`. – mfaani Sep 13 '18 at 14:20
  • https://stackoverflow.com/a/72325838. This might be helpful. You need to implement the didReceive response delegate method in the AppDelegate. Then you will be able to get the notification actions – Deepansh Jagga Feb 12 '23 at 20:07

0 Answers0