Skip to content

agent.notifications

Interface

Register 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): void

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

ParameterTypeDescription
callback(id: string, packageName: string, channelId: string, extras: any) => voidCallback 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): void

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

ParameterTypeDescription
notificationIdstringThe notification ID from the callback
shouldOpenNotificationbooleanWhether 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

NameTypeDescription
idstringUnique notification identifier
packageNamestringApp that sent the notification
channelIdstringNotification channel ID
extrasanyNotification 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.