Automation API

Android device automation

AgentNotifications

Interface

Handle system notifications

Access notification methods through agent.notifications. Register callbacks to receive notifications and process them.

AgentNotifications Interface
TypeScript
interface AgentNotifications {
setNotificationCallback(callback: NotificationCallback | null): void;
onProcessed(notificationId: string, shouldOpenNotification: boolean): void;
}

Methods

setNotificationCallback()

TypeScript
setNotificationCallback(callback: NotificationCallback | null): void

Registers a callback to receive system notifications. Pass null to unregister.

Parameters

NameTypeDescription
callback(id: string, packageName: string, channelId: string, extras: any) => voidCallback function or null to unregister

Examples

TypeScript
agent.notifications.setNotificationCallback((id, packageName, channelId, extras) => {
console.log("Notification from:", packageName);
console.log("Title:", extras.title);
console.log("Text:", extras.text);
// Process the notification
agent.notifications.onProcessed(id, false);
});
// Later, to stop receiving notifications:
agent.notifications.setNotificationCallback(null);

onProcessed()

TypeScript
onProcessed(notificationId: string, shouldOpenNotification: boolean): void

Call this after processing a notification to indicate it has been handled. Optionally open the notification.

Parameters

NameTypeDescription
notificationIdstringThe notification ID from the callback
shouldOpenNotificationbooleanWhether to open/click the notification

Examples

Basic notification handling
TypeScript
// Set up notification listener
agent.notifications.setNotificationCallback((id, packageName, channelId, extras) => {
console.log("Notification from:", packageName);
console.log("Title:", extras.title);
console.log("Text:", extras.text);
// Mark as processed without opening
agent.notifications.onProcessed(id, false);
});
Open specific notifications
TypeScript
agent.notifications.setNotificationCallback((id, packageName, channelId, extras) => {
// Only open notifications from specific app
if (packageName === "com.whatsapp") {
console.log("WhatsApp message:", extras.text);
agent.notifications.onProcessed(id, true); // Open the notification
} else {
agent.notifications.onProcessed(id, false); // Just dismiss
}
});
Extract verification codes
TypeScript
agent.notifications.setNotificationCallback((id, packageName, channelId, extras) => {
const text = extras.text || "";
// Look for verification codes in notifications
const codeMatch = text.match(/\b\d{4,6}\b/);
if (codeMatch) {
console.log("Found verification code:", codeMatch[0]);
// Store or use the code...
}
agent.notifications.onProcessed(id, false);
});

Complete Example

TypeScript
// Track notifications for a specific task
const receivedCodes = [];
// Set up listener
agent.notifications.setNotificationCallback((id, packageName, channelId, extras) => {
console.log("=== Notification Received ===");
console.log("Package:", packageName);
console.log("Channel:", channelId);
console.log("Title:", extras.title);
console.log("Text:", extras.text);
// Look for OTP codes
const text = (extras.title || "") + " " + (extras.text || "");
const otpMatch = text.match(/\b\d{6}\b/);
if (otpMatch) {
receivedCodes.push({
code: otpMatch[0],
from: packageName,
time: Date.now()
});
}
// Mark as processed
agent.notifications.onProcessed(id, false);
});
// Later, when you need the code:
if (receivedCodes.length > 0) {
const latestCode = receivedCodes[receivedCodes.length - 1];
console.log("Using code:", latestCode.code);
}
// Clean up when done
agent.notifications.setNotificationCallback(null);

Callback Types

NotificationCallback

TypeScript
type NotificationCallback = (
id: string,
packageName: string,
channelId: string,
extras: {
title?: string;
text?: string;
[key: string]: any;
}
) => void;

Callback Parameters

ParameterTypeDescription
idstringUnique notification identifier
packageNamestringApp that sent the notification
channelIdstringNotification channel ID
extrasanyNotification extras (title, text, etc.)

Tip

Remember to call agent.notifications.setNotificationCallback(null) when you no longer need to receive notifications to avoid memory leaks and unnecessary processing.