I had similar problem like yourself, here is how I solved this:
This is part of code from my page object where when clicking on useGPS, native Android notification is shown to allow or block GPS usage,
public Alert clickButtonUseGPSwithAlert() {
buttonUseGps.click();
Validate.action(getSessionInfo(), "click button 'Use a GPS'");
Alert alert = new Alert(getSessionInfo());
return alert;
}
and here is an overriden class Alert
,
import io.appium.java_client.MobileElement;
import io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import io.appium.java_client.pagefactory.WithTimeout;
import org.openqa.selenium.support.PageFactory;
import pageObjects.Screen;
import utils.Validate;
import java.util.concurrent.TimeUnit;
public class Alert extends Screen implements org.openqa.selenium.Alert {
@AndroidFindBy(id = "com.android.packageinstaller:id/dialog_container")
@WithTimeout(time = 3, unit = TimeUnit.SECONDS)
public MobileElement alertControl;
@AndroidFindBy(id = "com.android.packageinstaller:id/permission_message")
@WithTimeout(time = 3, unit = TimeUnit.SECONDS)
private MobileElement content;
@AndroidFindBy(id = "com.android.packageinstaller:id/permission_allow_button")
@WithTimeout(time = 3, unit = TimeUnit.SECONDS)
private MobileElement buttonAccept;
@AndroidFindBy(id = "com.android.packageinstaller:id/permission_deny_button")
@WithTimeout(time = 3, unit = TimeUnit.SECONDS)
private MobileElement buttonDismiss;
public Alert(SessionInfo sessionInfo){
super(sessionInfo);
PageFactory.initElements(new AppiumFieldDecorator(sessionInfo.getMobileDriver(), 3, TimeUnit.SECONDS), this);
WaitUtils.isElementPresent(sessionInfo.getMobileDriver(),alertControl,2);
if (!Util.areElementsLoaded(alertControl, content, buttonAccept, buttonDismiss)) {
setLoaded(false);
} else {
setLoaded(true);
}
Validate.isScreenLoaded(getSessionInfo(), this.isLoaded());
}
@Override
public void dismiss() {
buttonDismiss.click();
Validate.action(getSessionInfo(), "ALERT - click button 'Dismiss'");
}
@Override
public void accept() {
buttonAccept.click();
Validate.action(getSessionInfo(), "ALERT - click button 'Accept'");
}
@Override
public String getText() {
String value = content.getText();
Validate.action(getSessionInfo(), "ALERT - get content");
return value;
}
@Override
public void sendKeys(String s) {
}
}
Ignore Screen extending, just try using implementation of existing Alert (org.openqa.selenium.Alert package).
I know this is not solution 1:1 You have to tweak it, to incorporate into Your code, but main point is to try to override Alert and wait for element to appear, and than interact with it.
Hope this would help You,