Class: NotificationManager
The Notification Manager
is responsible for handling all tasks related to notifications within the system. It provides methods to fetch, process, and delete notifications, ensuring that the user is always up to date with important events. It integrates with the agent to handle notifications effectively and can be used to process updates, clear notifications, and retrieve new ones.
This manager is crucial for applications where real-time updates or notifications are required, such as handling credential offers, proof requests, or basic messages related to user interactions.
This concise description can be placed at the top of the Notification Manager
documentation.
Method: process
The process()
method is used to handle and process notifications based on their type. It retrieves the notification details, verifies its status, and processes it according to its type (e.g., basic message, credential offer, proof request). It also manages the lifecycle of the notification, ensuring it is marked as processed or read once handled.
-
Purpose: The purpose of the
process()
method is to process a specific notification, execute associated actions (e.g., respond to messages, handle proof requests, or credential offers), and update the notification's status in the system. -
Parameters:
id: number
— The unique identifier of the notification to process.
-
Returns:
Promise<OperationResult>
— Resolves to anOperationResult
object indicating whether the notification was successfully processed. If unsuccessful, it returns an error message.
Usage Example
This example demonstrates how to process a notification by its ID.
// Notification ID to process
const notificationId = 12345; // Hardcoded notification ID
// Process the notification
const result = await agent.notificationManager.process(notificationId);
if (!result.isSuccessful) {
console.error(`Failed to process notification. Error: ${result.error}`);
} else {
console.log('Notification successfully processed.');
}
Example Output (Success)
{
"isSuccessful": true
}
Example Output (Failure - Already Processed)
{
"isSuccessful": false,
"error": "Notification already processed. Skipping processing.",
"errorCode": "ItemProcessed"
}
Example Output (Failure - Notification Expired)
{
"isSuccessful": false,
"error": "Notification message process has expired.",
"errorCode": "OperationFailed"
}
Example Output (Failure - Notification Not Found)
{
"isSuccessful": false,
"error": "Notification not found.",
"errorCode": "RecordNotFound"
}
Common Use Cases
- Handling Notifications: Use this method to process different types of notifications, including credential offers, proof requests, and basic messages.
- Marking Notifications as Processed: Once a notification is processed, the method updates its status to ensure that it is not processed again.
Method: fetch
The fetch()
method is used to retrieve a list of notifications based on pagination parameters. It allows the user to specify the current page number and the number of notifications per page. The method orders the notifications by their creation date in descending order, ensuring that the most recent notifications are fetched first.
-
Purpose: The purpose of the
fetch()
method is to retrieve a paginated list of notifications from the system, providing flexibility to load notifications in smaller chunks for efficient handling. -
Parameters:
page: number
— (Optional) The current page number to retrieve. Defaults to1
.pageSize: number
— (Optional) The number of notifications to fetch per page. Defaults to10
.
-
Returns:
Promise<Notification[]>
— Resolves to an array ofNotification
objects, representing the fetched notifications.
Usage Example
This example demonstrates how to fetch notifications using the default page and page size.
// Fetch notifications from page 1 with 10 notifications per page
const notifications = await agent.notificationManager.fetch(1, 10);
console.log('Fetched notifications:', notifications);
Example Output
[
{
"id": 12345,
"title": "New Credential Offer",
"body": "You have a new credential offer.",
"createdAt": "2024-10-05T12:34:56Z",
"isRead": false,
"isProcessed": false
},
{
"id": 12344,
"title": "Proof Request",
"body": "Please provide proof of your identity.",
"createdAt": "2024-10-04T10:30:12Z",
"isRead": true,
"isProcessed": true
}
]
Common Use Cases
- Fetching Recent Notifications: Use this method to fetch the latest notifications from the system, with pagination for better performance.
- Loading Notifications in Chunks: By specifying the
page
andpageSize
, developers can load notifications incrementally, reducing load times for large sets of notifications.
Method: delete
The delete()
method is used to delete a notification by its unique identifier. This method removes a specific notification from the system based on the provided notificationId
.
-
Purpose: The purpose of the
delete()
method is to permanently remove a notification from the system using its unique identifier. This is useful for clearing out processed or irrelevant notifications. -
Parameters:
notificationId: number
— The unique identifier of the notification to delete.
-
Returns:
Promise<void>
— Resolves with no value if the notification is successfully deleted. If an error occurs, the method throws an exception.
Usage Example
This example demonstrates how to delete a notification using its ID.
// Notification ID to delete
const notificationId = 12345; // Hardcoded notification ID
// Delete the notification
await agent.notificationManager.delete(notificationId);
console.log(`Notification with ID ${notificationId} has been deleted.`);
Common Use Cases
- Removing Processed Notifications: Use this method to delete notifications that have already been processed or are no longer relevant.
- Clearing User-Specific Notifications: This method can be used to clear notifications for a specific user once they have been addressed.