AgentNotifications
InterfaceHandle 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): voidRegisters a callback to receive system notifications. Pass null to unregister.
Parameters
| Name | Type | Description |
|---|---|---|
callback | (id: string, packageName: string, channelId: string, extras: any) => void | Callback 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): voidCall this after processing a notification to indicate it has been handled. Optionally open the notification.
Parameters
| Name | Type | Description |
|---|---|---|
notificationId | string | The notification ID from the callback |
shouldOpenNotification | boolean | Whether to open/click the notification |
Examples
Basic notification handling
TypeScript
// Set up notification listeneragent.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 taskconst receivedCodes = [];
// Set up listeneragent.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 doneagent.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
| Parameter | Type | Description |
|---|---|---|
id | string | Unique notification identifier |
packageName | string | App that sent the notification |
channelId | string | Notification channel ID |
extras | any | Notification 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.