androidiphonepush-notificationappium

How to open the push notifications and read the text in Android/iOS device from Appium?


1.When the device receives push how to automate in Appium so that it will open the push notification and read the text.


Solution

  • i am using following code to tap notification with some needed text. you can use it and do whatever needed (did not update code it long time. some parts can be re-written much better. but still working with every phone i tested):

    Page object first:

    public class NativeNotificationPage extends Page {
    
        @AndroidFindBy(id= "com.android.systemui:id/notification_panel")
        private List<AndroidElement> notificationPanel;
        //settings data
        @HowToUseLocators(androidAutomation = LocatorGroupStrategy.ALL_POSSIBLE)
        @AndroidFindBy(id = "com.android.systemui:id/clear_all_button")
        @AndroidFindBy(id = "com.android.systemui:id/dismiss_text")
        private List<AndroidElement> clearAllBtn;
        //last items
        @AndroidFindBy(id = "com.android.systemui:id/latestItems")
        private List<AndroidElement> lastItemsContainer;
        //events data
        @AndroidFindBy(id = "android:id/status_bar_latest_event_content")
        private List<AndroidElement> lastItemsContent;
        @AndroidFindBy(id = "android:id/title")
        private List<AndroidElement> itemTitle;
        String itemTitle_Locator_Text = "android:id/title";
    
    
        @HowToUseLocators(androidAutomation = LocatorGroupStrategy.ALL_POSSIBLE)
        @AndroidFindBy (id = "android:id/big_text")
        @AndroidFindBy (id = "android:id/text")
        private List<AndroidElement> itemText;
        String itemText_Phone_Locator_Text = "android:id/text";
        String itemText_Tablet_Locator_Text = "android:id/big_text";
        @AndroidFindBy(id = "android:id/time")
        private List<AndroidElement> itemTime;
    
    
        public NativeNotificationPage(WebDriver driver) {
            super(driver);
        }
    
        public boolean isNativeNotificationPage() {
            System.out.println("  check 'Notification' Screen loaded");
            boolean bool;
            setFastLookTiming();
            bool = !notificationPanel.isEmpty();
            setDefaultTiming();
            return bool;
        }
    
        public boolean isNativeNotificationPage(int sec) {
            System.out.println("  check 'Notification' Screen loaded");
            boolean bool;
            setLookTiming(sec);
            bool = !notificationPanel.isEmpty();
            setDefaultTiming();
            return bool;
        }
    
        public boolean isClearAllBtnLoaded() {
            System.out.println("  check 'Clear' button loaded");
            boolean bool;
            setLookTiming(3);
            bool = !clearAllBtn.isEmpty();
            setDefaultTiming();
            return bool;
        }
    
        public int getLastItemsContentSize() {return lastItemsContent.size();}
    
        public String getItemTitle(int num) {
            try {
                return lastItemsContent.get(num).findElement(MobileBy.className("android.widget.TextView")).getText();
            } catch (Exception e) {
                return null;
            }
        }
    
        public String getItemText(int num) {
            if (isPhone()) {
                List<MobileElement> item = lastItemsContent.get(num).findElements(MobileBy.className("android.widget.TextView"));
                String tmp = null;
                for (int i=1;i<item.size();i++) {
                    if (tmp == null)
                        tmp = item.get(i).getText();
                    else
                        tmp = tmp + "," +item.get(i).getText();
                }
                return tmp;
            } else {
                setLookTiming(3);
                if (lastItemsContent.get(num).findElements(MobileBy.id(itemText_Tablet_Locator_Text)).isEmpty()) {
                    setDefaultTiming();
                    return lastItemsContent.get(num).findElement(MobileBy.id(itemText_Phone_Locator_Text)).getText();
                } else {
                    setDefaultTiming();
                    return lastItemsContent.get(num).findElement(MobileBy.id(itemText_Tablet_Locator_Text)).getText();
                }
            }
        }
    
        public boolean tapClearAllBtn() {
            System.out.println("  tap 'Clear' button");
            return tapElementInList_Android(clearAllBtn, 0);
        }
    
        public boolean tapNotificationByNum(int num) {
            return tapElementInList_Android(lastItemsContent, num);
        }
    
    }
    

    Now in test code:

        public Boolean tapNotificationByTitleAndText_Android(String neededTitle, String neededText, AppiumDriver driver) {
                ((AndroidDriver) driver).openNotifications();
                sleep(1);
                nativeNotificationPage = new NativeNotificationPage(driver);
                assertTrue("Native notification page is NOT loaded", nativeNotificationPage.isNativeNotificationPage());
    
                int itemsListSize = nativeNotificationPage.getLastItemsContentSize();
                System.out.println("  number of notifications is: " + itemsListSize);
                assertTrue("Number of notifications is 0", itemsListSize != 0);
                String title, text;
    
                for (int i = 0; i < itemsListSize; i++) {
                    title = nativeNotificationPage.getItemTitle(i);
                    text = nativeNotificationPage.getItemText(i);
                    System.out.println("   notification title is: " + title);
                    System.out.println("   notification text is: " + text);
                    .....
                    return nativeNotificationPage.tapNotificationByNum(i);
                }
    
                return false;
            }