agent.notifications
InterfaceRegister callbacks to receive and process system notifications.
AgentNotifications Interface
interface AgentNotifications {
setNotificationCallback(callback: NotificationCallback | null): void;
onProcessed(notificationId: string, shouldOpenNotification: boolean): void;
}setNotificationCallback
Signature
setNotificationCallback(callback: NotificationCallback | null): voidRegisters a callback to receive system notifications. Pass null to unregister.
| Parameter | Type | Description |
|---|---|---|
callback | (id: string, packageName: string, channelId: string, extras: any) => void | Callback function or null to unregister |
Example
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
Signature
onProcessed(notificationId: string, shouldOpenNotification: boolean): voidCall this after processing a notification to indicate it has been handled. Optionally open the notification.
| Parameter | Type | Description |
|---|---|---|
notificationId | string | The notification ID from the callback |
shouldOpenNotification | boolean | Whether to open/click the notification |
Basic notification handling
// 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
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
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
// 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);NotificationCallback
NotificationCallback
type NotificationCallback = (
id: string,
packageName: string,
channelId: string,
extras: {
title?: string;
text?: string;
[key: string]: any;
}
) => void;Callback Parameters
| Name | 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.) |
Remember to call
agent.notifications.setNotificationCallback(null) when you no longer need to receive notifications to avoid memory leaks and unnecessary processing.