All posts

Automate Push Notification Testing on Android with Phoma

Automate Push Notification Testing on Android with Phoma

Why push‑notification testing matters

Push notifications are a primary engagement channel for most Android apps. A broken payload, missing deep‑link handling, or a UI that doesn’t react to the notification can silently degrade user retention. Manual testing is slow, flaky, and impossible to repeat on dozens of devices. Phoma lets you turn the whole cycle — send, receive, act, assert — into a single visual flow that runs on any number of phones attached to your Windows PC.

Prerequisites

  1. Phoma installed on Windows with at least one Android device connected (USB or wireless ADB).
  2. Notification access granted to the app under test (Settings → Apps → Special access → Notification access).
  3. FCM server key or any HTTP endpoint that can push a test message to the device (you can also use the Firebase console for ad‑hoc sends).
  4. Phoma Team/Business plan if you want to run the same flow on a device pool simultaneously.

Overview of the flow

Step Purpose Phoma step type
1 Send a test push via HTTP request HTTP Request
2 Wait for the notification to appear in the shade Wait for Element (selector targets android.service.notification.NotificationListenerService)
3 Expand the notification and tap the action button Tap (selector on the action’s android:id/action_button)
4 Verify the app opens on the expected screen Wait for Element + Text Assertion
5 Capture a screenshot for the run report Screenshot
6 (Optional) Loop over a list of payload variants Loop + Variable

Step‑by‑step construction

1. Send the push with an HTTP Request step

Add an HTTP Request step at the top of the flow. Use the FCM legacy HTTP API (or your own push gateway). The request body is a JSON payload; you can parameterise the title, body, and data fields with flow variables so the same flow can test multiple scenarios.

{
  "to": "{{device_fcm_token}}",
  "notification": {
    "title": "{{push_title}}",
    "body": "{{push_body}}"
  },
  "data": {
    "deep_link": "{{deep_link_url}}",
    "test_id": "{{test_id}}"
  }
}

Set Method → POST, URL → https://fcm.googleapis.com/fcm/send, Headers → Authorization: key={{fcm_server_key}} and Content-Type: application/json. Store the server key and each device’s registration token in Phoma’s Variables panel (they are encrypted at rest).

2. Wait for the notification to appear

Android surfaces notifications in the system shade, which is part of the NotificationListenerService UI tree. Add a Wait for Element step with a selector that matches the notification’s package name and a stable attribute such as android:text containing the push title variable.

// Example selector (UIAutomator syntax)
new UiSelector().packageName("com.android.systemui").textContains("{{push_title}}")

Set Timeout to 30 s and Poll Interval to 1 s. If the notification never arrives, the flow fails fast and the run report shows the HTTP response from step 1 for quick debugging.

3. Expand and tap the action button

Notifications may be collapsed. Use a Tap step on the expand affordance (android:id/expand_button) followed by a second Tap on the action button you care about (e.g., “Open”). Because the shade UI is transient, chain the two taps without an explicit wait; Phoma’s built‑in retry logic handles the brief animation.

// Expand
new UiSelector().resourceId("android:id/expand_button")
// Action button (usually the first action)
new UiSelector().resourceId("android:id/action_button").instance(0)

4. Verify the deep‑link landing screen

After the tap, the app should launch and navigate to the screen encoded in deep_link. Add a Wait for Element that targets a unique element on that screen — for example a toolbar title or a RecyclerView item with the test_id you sent in the data payload.

new UiSelector().resourceId("com.example.app:id/toolbar_title").text("{{expected_screen_title}}")

Follow with a Text Assertion step to confirm the exact copy, and a Screenshot step to capture visual evidence. The screenshot is automatically attached to the run report and can be compared across devices.

5. Parameterise multiple payloads with a Loop

Create a Variable named payloads that holds a JSON array of test cases (title, body, deep_link, expected_screen_title). Add a Loop step that iterates over payloads. Inside the loop, map each array entry to the flow variables used in steps 1‑4 (push_title, push_body, deep_link_url, expected_screen_title, test_id). This yields a single flow that validates dozens of notification permutations in one run.

[
  {"push_title":"Promo", "push_body":"20% off", "deep_link_url":"app://promo/20", "expected_screen_title":"Promo Details", "test_id":"promo_20"},
  {"push_title":"Alert", "push_body":"Server down", "deep_link_url":"app://alert/server", "expected_screen_title":"Incident View", "test_id":"alert_server"}
]

6. Run on many phones at once

If you have a device pool (Team/Business plan), open the Run Configuration dialog, select All devices in pool, and enable Parallel execution. Phoma will dispatch the same flow to every connected phone, each using its own FCM token variable. The run dashboard aggregates pass/fail per device, making it trivial to spot a model‑specific regression.

Handling common pitfalls

Symptom Root cause Fix
Notification never appears Device not registered for FCM or token stale Refresh token in app, update device_fcm_token variable, ensure google-services.json is current.
Flow taps wrong action button Multiple actions, index shifts Use a selector that matches the action’s android:text (e.g., text("Open")) instead of instance(0).
Deep link opens a fallback screen Intent filter missing or data schema mismatch Verify AndroidManifest.xml declares <data android:scheme="app" android:host="promo"/>.
Screenshot shows system shade still open Tap executed before animation finished Insert a short Wait step (500 ms) after the expand tap, or enable Wait for UI idle in the step settings.

Extending the pattern

  • Rich notifications – Add a step that reads the android:progress or android:bigText via Read Text and asserts the formatted content.
  • Silent pushes – Omit the notification block, send only data, then start the app with an Open App step and verify background processing (e.g., local database update) using a Run Shell step (adb shell dumpsys package com.example.app).

Checklist before you ship

  • All device FCM tokens are current and stored as encrypted variables.
  • Notification access is granted on every test phone.
  • Selectors use stable attributes (resourceId, text, contentDescription) rather than fragile indices.
  • Loop payloads cover happy path, empty payload, malformed deep link, and locale variations.
  • Run the flow on at least three device models (low‑end, flagship, tablet) in parallel.
  • Review the aggregated run report; any flaky step should be hardened with a longer timeout or an explicit wait.

TL;DR

  1. Send a push via HTTP Request (parameterised).
  2. Wait for the system notification using a UIAutomator selector on the shade.
  3. Expand and tap the desired action button.
  4. Assert the app lands on the correct deep‑link screen (text + screenshot).
  5. Loop over a JSON array of test cases to cover many scenarios in one flow.
  6. Run in parallel on a device pool for instant cross‑device coverage.

With this pattern you turn a traditionally manual, error‑prone QA task into a repeatable, version‑controlled Phoma flow that runs on any number of Android phones from a single Windows workstation.