Social Notifications
Applications such as NEAR Social and the NEAR Dev Portal allow components to send notifications to their users.
Notifications are great to inform users in real time that something has happened, and can be easily incorporated into any web app.
Sending Notifications
Notifications are implemented as a particular case of indexed actions.
This means that to send a notification we just need to index
the notify
action for the indexer, with a key
and a value
.
- The
key
tells which account to notify. - The
value
includes the notification type and the item being notified of.
In this example, the account executing the code is notifying mob.near
that they liked their social post created at the block height 102169725
.
Notification Types
Since notifications are indexed actions, anyone can create their own kind of notification.
While there is no standard for notifications, we recommend using the following types:
- Custom
- Like
- Comment
- Follow
Social.set({
index: JSON.stringify({
notify: {
key: "mob.near",
value: {
type: "custom",
message: "A message in the notification",
widget: "The widget to open when clicking on the notification",
params: { parameters: "to pass to the widget", ... },
},
}
})
});
Note: currently, only dev.near.org implements custom notifications
Social.set({
index: JSON.stringify({
notify: {
key: "mob.near",
value: {
type: "like",
item: {
type: "social",
path: "mob.near/post/main",
blockHeight: 102169725
}
}
}
})
})
Reference: LikeButton
Social.set({
index: JSON.stringify({
notify: {
key: "nearhacks.near",
value: {
type: "comment",
item: {
type: "social",
path: "nearhacks.near/post/main",
blockHeight: 102224773
}
}
}
})
})
Reference: CommentButton
Social.set({
index: JSON.stringify({
notify: {
key: "mob.near",
value: {
type: "follow",
}
}
})
})
Reference: FollowButton
There is no standard for notifications, but you can contribute to create one in this public discussion.
Parsing Notifications
In order to get all notifications for an user, we make a call to the Social indexer.
Please notice that anyone can create a notification for the user, and thus some form of filtering might be needed.
You can also check how the Notifications Page is implemented.