How to reverse the Notification (Local or Push) tap behaviour

I want to change the notification tap behaviour. As we know that default behaviour of notification is when we tap or touch on notification then it opens the app and when we press and hold (long press) the notification then its shows the notification preview (should become interactive notification) and we can interact through notification without opening the app.


But I want to reverse the default behaviour of notification i.e. when we tap or touch on the notification then it should open the notification preview (should become interactive notification) and we can interact through notification without opening the and when we press and hold the notification then it should open the app.

Could you please guild me how can I implement this notification feature.

To reverse the default behavior of notifications on iOS, where tapping opens the notification preview (interactive notification) and pressing and holding opens the app, you can achieve this by customizing the notification's content and actions.

Here are the steps to implement this:

  1. Create a Custom Notification Content Extension: You'll need to create a Notification Content Extension to customize the appearance and behavior of your notifications. Follow these steps:

    • In Xcode, go to "File" -> "New" -> "Target..."
    • Select "Notification Content Extension" from the list.
    • Name your extension and click "Finish."
  2. Customize the Notification Content: In your Notification Content Extension, you can customize the content of your notification. You can create a custom UI and handle interactions within this extension.

  3. Customize the Notification Actions: You can define custom notification actions for your notification. These actions can be used to open the app when pressed and held. Define these actions in your main app's code:

    import UserNotifications
    
    // Define custom actions
    let openAppAction = UNNotificationAction(
        identifier: "OpenAppAction",
        title: "Open App",
        options: [.foreground]
    )
    
    // Create a category that includes the custom actions
    let notificationCategory = UNNotificationCategory(
        identifier: "CustomCategory",
        actions: [openAppAction],
        intentIdentifiers: [],
        options: []
    )
    
    // Register the category with the notification center
    UNUserNotificationCenter.current().setNotificationCategories([notificationCategory])
    
  4. Handle Actions in the Extension: In your Notification Content Extension, you can implement code to handle the custom actions. For example, when the "Open App" action is triggered, you can open the app:

    func didReceive(_ notification: UNNotification) {
        // Handle the notification content
        // ...
    
        // Handle the custom actions
        if let action = notification.request.content.categoryIdentifier {
            switch action {
            case "CustomCategory":
                if notification.request.identifier == "OpenAppAction" {
                    // Handle the "Open App" action
                    // Open your app here
                }
            default:
                break
            }
        }
    }
    

By following these steps, you can reverse the default behavior of notifications on iOS, making tapping open the notification preview and pressing and holding open the app. Remember to customize the notification content and actions to suit your app's specific needs.

it didn't worked for me. Apple provided 3 properties for Options[] i.e. .authenticationRequired, .destructive and .foreground. I'm curious how we can differentiate trigger action for Simple Tap and Press and hold. Please refer the screenshot attached

.

I want to understand how can we preview the the notification when we simple tap the notification. Thanks in advance.

How to reverse the Notification (Local or Push) tap behaviour
 
 
Q