test_notification
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

Siren JS SDK

The Siren JS SDK for In-App Notifications enhances Siren's capabilities by providing users with advanced notification management functionalities within their javascript applications. This feature ensures a seamless integration process, robust error handling, and compatibility with Siren's existing ecosystem

Installation

You can install the js sdk from npm

npm install @sirenapp/js-sdk

or from yarn

yarn add @sirenapp/js-sdk

or from cdn

<script src="https://siren-js.sirenapp.io/siren-js-umd-sdk.js"></script>

Usage

Initialize the SDK by creating a class instance as follows

import Siren from "@sirenapp/js-sdk"

const sirenInstance = new Siren({
        token: "your-user-token",
        recipientId: "your-recipient-id",
        onError: (error) => {
            # error callback function
        });
        actionCallbacks:{
            onNotificationReceived?: (response: NotificationsApiResponse) => {
                 # callback function on getting notifications
            };
            onUnViewedCountReceived?: (response: UnviewedCountApiResponse) => {
                 # callback function on getting unviewed count
            };
            onStatusChange?: (status: 'SUCCESS' | 'FAILED' | 'PENDING') =>{
                  # callback function triggered when token verification status changes
            }
        }

All the exposed methods can be accessed using sirenInstance object. For example,to fetch all notifications,

const response = await sirenInstance.fetchAllNotifications({ page: 0, size: 10 })

Siren constructor accepts the following arguments

Property Description Type Optional
token Siren user token string false
recipientId Siren recipient id string false
onError To receive error call backs when there is any error function false
actionCallbacks An object used to specify the callbacks triggered upon fetching the notifications or the unviewed count dynamically object true

Siren Methods

1. verifyToken

This method verifies the validity of the given tokens (recipientId and userToken).This method is called automatically while creating the instance . Once the verification is successful, the remaining exposed methods can be accessed.

await sirenInstance.verifyToken();

2. fetchUnviewedNotificationsCount

This method retrieves the count of unviewed notifications.

const { unviewedCount } = await sirenInstance.fetchUnviewedNotificationsCount()

3. fetchAllNotifications

This method retrieves list of notifications in a paginated manner.

const notifications = await sirenInstance.fetchAllNotifications({ page: 0, size: 15, start: '', end: '', isRead: false });
Argument Description Type Optional Default
page Current page number false 0
size Number of items fetched number false 10
start Accepts an ISO date string to filter notifications created after the specified date. By default, only the first 20 notifications will be fetched
eg: 2024-02-19T06:37:33.792+00:00
string false null
end Accepts an ISO date string to filter notifications created before the specified date. By default, only the first 20 notifications will be fetched
eg: 2024-02-19T06:37:33.792+00:00
string false null
isRead Filter to fetch read or unread notifications. If not specified, it retrieves both read and unread notifications boolean false null

Response

 interface Notifications = {
    id: string;
    createdAt?: string;
    message: {
        channel: string;
        header: string;
        subHeader: string;
        body: string;
        actionUrl: string;
        avatar: {
            imageUrl: string;
            actionUrl: string | null;
        }
        additionalData: string;
    }
    requestId: string;
    isRead: boolean;
}[]

4. startRealTimeNotificationFetch

This method initiates the real-time fetching of notifications

await sirenInstance.startRealTimeNotificationFetch({ page: 0, size: 15, start: '', end: '', isRead: false });

The notifications received can be subscribed using the onNotificationReceived actionCallback.

Argument Description Type Optional Default
page Represents the current page number false 0
size The number of items to be fetched in a single call number false 10
start Accepts an ISO date string to filter notifications created after the specified date. By default, only the first 20 notifications will be fetched string false null
end Accepts an ISO date string to filter notifications created before the specified date. By default, only the first 20 notifications will be fetched string false null
isRead Filter to fetch read or unread notifications. If not specified, it retrieves both read and unread notifications number false null

5. startRealTimeUnviewedCountFetch

This method initiates the real-time fetching of unviewed notification count.The count can be subscribed using the onUnViewedCountReceived actionCallback. It returns the count after the last markNotificationsAsViewed function call.

 await sirenInstance.startRealTimeUnviewedCountFetch();

6. stopRealTimeNotificationFetch

This method stops the fetching of realtime notifications

await sirenInstance.stopRealTimeNotificationFetch();

7. stopRealTimeUnviewedCountFetch

This method stops the fetching of realtime unviewed count

await sirenInstance.stopRealTimeUnviewedCountFetch();

8. markAsReadById

This method marks the notification as read. It accepts a notification id as an argument.

await sirenInstance.markAsReadById("your-notification-id");

9. markNotificationsAsReadByDate

This method marks the notifications as read till the given date.
It accepts an ISO date string as an argument.

await sirenInstance.markNotificationsAsReadByDate("2011-10-05T14:48:00.000Z");

10. deleteNotificationById

This method deletes a notification. It accepts a notification id as an argument.

await sirenInstance.deleteNotificationById("your-notification-id");

11. deleteNotificationsByDate

This method deletes the notifications till the given date.
It accepts an ISO date string as an argument

await sirenInstance.deleteNotificationsByDate("2011-10-05T14:48:00.000Z");

12. markNotificationsAsViewed

This method marks the notifications as viewed till the given date. This sets the unviewed count as 0
It accepts an ISO date string as an argument

await sirenInstance.markNotificationsAsViewed("2011-10-05T14:48:00.000Z");

Package Sidebar

Install

npm i test_notification

Weekly Downloads

35

Version

1.1.0

License

MIT

Unpacked Size

107 kB

Total Files

35

Last publish

Collaborators

  • bilta-john