driver.getClipboardText() does not work on Google Pixel 4/Android 11

tried:
driver.getClipboard(ClipboardContentType.PLAINTEXT); // get plaintext
driver.getClipboardText();
result - empty string
[debug] [W3C (34d29e57)] Calling AppiumDriver.getClipboard() with args: [“plaintext”,“34d29e57-3a22-402a-8fba-d3bfaaea4497”]

[debug] [ADB] Getting the clipboard content

[debug] [ADB] Running ‘/Users/***/Library/Android/sdk/platform-tools/adb -P 5037 -s 99111FFAZ00CE8 shell settings get secure default_input_method’

[debug] [ADB] Running ‘/Users/***/Library/Android/sdk/platform-tools/adb -P 5037 -s 99111FFAZ00CE8 shell ime set io.appium.settings/.AppiumIME’

[debug] [ADB] Running ‘/Users/***/Library/Android/sdk/platform-tools/adb -P 5037 -s 99111FFAZ00CE8 shell am broadcast -n io.appium.settings/.receivers.ClipboardReceiver -a io.appium.settings.clipboard.get’

[debug] [ADB] Running ‘/Users/***/Library/Android/sdk/platform-tools/adb -P 5037 -s 99111FFAZ00CE8 shell ime set com.google.android.inputmethod.latin/com.android.inputmethod.latin.LatinIME’

[debug] [W3C (34d29e57)] Responding to client with driver.getClipboard() result: “”

[HTTP] <-- POST /wd/hub/session/34d29e57-3a22-402a-8fba-d3bfaaea4497/appium/device/get_clipboard 200 167 ms - 12

[HTTP]

[HTTP] --> POST /wd/hub/session/34d29e57-3a22-402a-8fba-d3bfaaea4497/appium/device/get_clipboard

[HTTP] {“contentType”:“plaintext”}

[debug] [W3C (34d29e57)] Calling AppiumDriver.getClipboard() with args: [“plaintext”,“34d29e57-3a22-402a-8fba-d3bfaaea4497”]

[debug] [ADB] Getting the clipboard content

[debug] [ADB] Running ‘/Users/***/Library/Android/sdk/platform-tools/adb -P 5037 -s 99111FFAZ00CE8 shell settings get secure default_input_method’

[debug] [ADB] Running ‘/Users/***/Library/Android/sdk/platform-tools/adb -P 5037 -s 99111FFAZ00CE8 shell ime set io.appium.settings/.AppiumIME’

[debug] [ADB] Running ‘/Users/***/Library/Android/sdk/platform-tools/adb -P 5037 -s 99111FFAZ00CE8 shell am broadcast -n io.appium.settings/.receivers.ClipboardReceiver -a io.appium.settings.clipboard.get’

[debug] [ADB] Running ‘/Users/***/Library/Android/sdk/platform-tools/adb -P 5037 -s 99111FFAZ00CE8 shell ime set com.google.android.inputmethod.latin/com.android.inputmethod.latin.LatinIME’

[debug] [W3C (34d29e57)] Responding to client with driver.getClipboard() result: “”

I was having a similar issue where tests checking the clipboard contents were failing only on Android 11 devices. Solved by running this app (which is the one reading from the clipboard) in the foreground. I’m using the Ruby client library:

def get_clipboard_content
    # On Android 11 and higher, the app reading from the clipboard must be in the foreground
    @driver.start_activity(
      app_package: 'io.appium.settings',
      app_activity: '.Settings'
    )
    @driver.get_clipboard
end