Skip to main content

Notifications

The collab client comes with browser notification support. Browser notifications are the popups that the browser displays when an event of interest happens (invited to a document, new message, etc).

To enable browser notifications, you must pass a notificationHandler to the client constructor.

const client = new CollabClient({
...otherOptions,
notificationHandler: (event) => {
}
})

A notificationHandler handler is a function that accepts an event, and uses that event to display a notification. The events are documented below.

We recommend using our CollabClient.defaultNotificationHandler, as it has all the functionality needed for browser notifications built right in.

Events#

Currently there are 2 types of events: an invite event and a new message event. The type of event can be determined from the event object passed into the notificationHandler.

const client = new CollabClient({
...otherOptions,
notificationHandler: (event) => {
if(event.type === 'invite') {
// trigger an invite notification
}
if(event.type === 'message') {
// trigger a new message notification
}
}
})

New invite event#

The invite event comes with the following data:

{
type: 'invite';
documentName: string;
documentId: string;
sentBy: string;
}

You can use this data to display a message such as ${sentBy} has invited you to collaborate on ${documentName}!

New message event#

The message event comes with the following data:

{
type: 'message';
documentName: string;
documentId: string;
annotationId: string;
messageText: string;
annotationType: string;
sentBy: string;
}

In some cases, messageText may be empty. This is due to the fact that most annotations do not contain body text, but we still consider them new messages.

Default handler#

The default notificationHandler is a nice default that implements all the functionality needed for browser notifications (permissions, messaging, etc).

To use it, call the static defaultNotificationHandler and pass the result to the notificationHandler constructor option.

import CollabClient from '@pdftron/collab-client'
const client = new CollabClient({
...otherOptions,
notificationHandler: CollabClient.defaultNotificationHandler({
onClick: (event) => {},
getText: (event) => {}
})
})

defaultNotificationHandler takes and object with two optional properties, onClick and getText.

onClick#

onClick is a function that gets called when the browser notification is clicked. It is passed the notification event as a parameter.

This could be used to route your user to a specific document, or load the document in the client when it's clicked.

import CollabClient from '@pdftron/collab-client'
const client = new CollabClient({
...otherOptions,
notificationHandler: CollabClient.defaultNotificationHandler({
onClick: (event) => {
const { documentId } = event;
client.loadDocumentById(documentId)
},
})
})

If onClick is not passed, nothing will happen when the notification is clicked.

getText#

getText is a function that accepts the notification event as a parameter, and returns what the title and body of the notification should be.

The function should return an object with the following shape:

{
title: string,
body: string
}

This allows you to customize the text displayed in the notification.

import CollabClient from '@pdftron/collab-client'
const client = new CollabClient({
...otherOptions,
notificationHandler: CollabClient.defaultNotificationHandler({
getText: (event) => {
if(event.type === 'invite') {
return {
title: `New invite recieved from ${event.sentBy}`,
body: `Click here to view ${event.documentName}`
}
}
},
})
})