How to use Appium to interact with an element that remains visible on the UI for 2-3 seconds?

I need to tap on an element that hardly stays for 3 seconds on the UI. Since it is vanishing from the UI within 3 seconds, my test is getting failed while finding the element. Is there a way to deal with it using appium?

Scenario -

  1. User1 generates a notification for User2
  2. User2 needs to find the element and tap on it

Please provide more details on your actual environment

@mykola-mokhnach We have host connected to the phone which runs appium , and controls ui elements by sending command to webdriver agent that is running on the phone.

  1. is this iOS/Android/Both?
  2. how you trying to search now?
  3. give example of page source when this element visible. include also few lines before and and after needed element.

We are seeing this issue in both. Currently, using resource-id for Android to locate this element.
if we take example of Teams app, where we receive a new chat we get a notification or a missed call notification in the app UI which stays for quite few seconds only, how to capture that elements?

the fastest way I know e.g. for android

// Java
        boolean result = false;
        long startTime = System.currentTimeMillis();
        do {
            try {
                // id has package name
                result = tap(driver.findElement(AppiumBy.id("id_with_package_name")));
                // or slightly slower
                // result = tap(driver.findElement(AppiumBy.androidUIAutomator("new UiSelector().resourceId(\"any_id\")")));
                // or we can disable add package name in appium settings and use first way
                if (result)
                    break;
            } catch (Exception ignored) {
            }
            sleep(100); // prevent too often search
        } while (System.currentTimeMillis() < startTime + 10 * 1000); // 10 sec MAX wait
        Assert.assertTrue(result, "Tap PopUp FAILED");
        
        // tap() and sleep() use any your implementation you know