Fetching and receiving notifications
This example component uses the Notification client to subscribe to Notifications sent into Finsemble.
Subscribe
When you set up the notification client to subscribe to notifications, the client receives all new notifications. All updated statuses to the actions are always returned.
The first thing you'll need to do is import the NotificationClient and Subscription classes:
import NotificationClient from "../finsemble-notifications/services/notification/notificationClient";
import Subscription from "../finsemble-notifications/types/Notification-definitions/Subscription";
The next step is to instantiate them:
// Setup up the client
const client = NotificationClient;
// Set up the subscription
const subscription = new Subscription();
subscription.onNotification = (notification) => {
// This will run whenever a notification is received.
// Do something with the notification. Use the notification.id to check
// if it's an updated notification or new one.
};
// Subscribe for notifications
let subscriptionId;
client.subscribe(subscription).then((subId) => (subscriptionId = subId));
Subscribe with filter
You might want to receive only some of notifications instead of all of them. You can do this by putting a filter on the subscription.
Import and instantiate the filter:
import Filter from "../../types/Notification-definitions/Filter";
const subscription = new Subscription();
subscription.filter = new Filter();
// Only return notifications with notification.type == 'chat-notification' and
// notification.source == 'slack' (For an OR match push two objects with the required matchers)
subscription.filter.include.push({
type: "chat-notification",
source: "slack",
});
// Subscribe as normal
client.subscribe(subscription);
It's also possible to do deep matches:
// return only notifications that have notification.meta.customField == 'hello'
subscription.filter.include.push({
"meta.customField": "hello",
});
Note: You can use a filter.exclude
array to exclude certain groups of notifications from being subscribed to.
Exclude rules have precedence over include rules.
Additional filter matchers
Primitives
A primitive is an object with properties that are matched, for example:
{name:"John"}
- primitive that checks that the name field is equal to "John"
Multiple fields in a primitive are, by default, joined by logical AND. See under Modifiers to change this.
{name:"John",age:30}
- primitive that checks that the name field is equal to "John" AND that the age field is equal to 30
The name of a field in a primitive is always the name of the field to match in the record. The value can be one of:
- Basic types: string, number, date - will match directly against the value in the record. Case is ignored in string matches.
- Array: will match against any one of the values in the array. See below.
Primitives search against individual values and against one or more matches in an array. So the search
{name:"John"}
will match any of these objects:
{name:"John"}
{name:["John","jim"]}
{name:["jim","John"]}
Deep searching
In addition to searching at the top level, you also can do deep searching on an object using
dot-notation. So if you want to match on the object {city: {Montreal: true}}
then you can search:
{"city.Montreal": true}
The above is a search primitive that checks that the field "city" has an object as its value, which in turn has a key
"Montreal" with a value of true
. You can go as deep as you want. The following is a completely valid deep-search
primitive:
{"country.province.city.street":"Dorchester Blvd"}
Any modifiers that apply to simple primitives apply to deep fields as well.
Deep searching arrays
Deep searching is not limited to objects embedded in objects. You can have arrays of objects embedded in objects. You even can have arrays of objects embedded in arrays of objects embedded in... (you get the idea!).
Thus, the search primitive {"name.cars.hp":{from:200}}
will match any of the following:
{cars: {brand: 'porsche',hp:450}}
{cars: [{brand: 'bmw',hp:250},{brand: 'lada',hp:10}]}
matches the 'bmw' but not the 'lada', therefore the whole object matches
Array primitive
If the value of a field in a primitive is an array, then it will accept a match of any one of the array values.
{name:["John","Jack"]} // accepts any record where the name field matches 'John' or 'Jack'
Additionally, if the target record also has an array, it will accept a match if any one of the values in the array of the record matches any one of the values in the array of the search term.
{name:["John","Jack"]}
will match any of these:
{name:"John",phone:"+12125551212"}
{name:"Jack",location:"Canada"}
{name:["John","Jim"],company:"Hot Startup"}
Perform actions
When you receive a notification, you want to perform one of the actions attached to it.
// Use the client to perform the action
let actionToPerform = notification.actions[0];
client.performAction(notification, actionToPerform);
// All subscribers will receive the updated action state