Instagram-style notification inbox
This recipe builds an in-app notification inbox like the one in Instagram: a bell with an unread badge, a list that groups similar notifications ("Jana and 12 others liked your post"), items that are marked read when tapped, real-time updates and mobile push notifications.
Everything here uses the built-in notification feed group, so there is no feed group to set up:
- Each user's notifications live in the feed
notification:<user id>. - Stream adds a notification activity to that feed when someone follows the user, reacts to or comments on their activity, or mentions them, as long as the action is sent with
create_notification_activity: true. - The group aggregates notifications for the same target, type and day into one item, and tracks which items each user has seen and read.
Stream delivers in-app notifications and mobile push. It doesn't send email or SMS. To reach users on those channels, listen for the same events with webhooks and send them through your own provider.
1. Create each user's notification feed
Stream adds a notification only to a notification feed that already exists. If the recipient has no notification feed yet, the follow, comment or reaction still succeeds, but no notification is created for them, no error is returned, and the notification isn't added later.
Create the feed when the user signs up, by calling getOrCreate on notification:<user id> from your server. It's the same call that reads the inbox in step 3. Creating it from the client when the user first opens your app also works, but the user misses notifications for anything that happens before then.
2. Create notifications when users interact
Pass create_notification_activity: true when a user follows someone, comments, or reacts. The recipient's notification feed gets a notification activity with a notification_context that describes what happened (the trigger) and what it happened to (the target).
// Eric follows Jane
await ericTimeline.follow(janeFeed, {
create_notification_activity: true,
});
// Eric comments on Jane's activity
await ericClient.addComment({
comment: "Agree!",
object_id: janeActivity.id,
object_type: "activity",
create_notification_activity: true,
});
// Eric reacts to Jane's activity
await ericClient.addActivityReaction({
activity_id: janeActivity.id,
type: "like",
create_notification_activity: true,
});
// Eric reacts to a comment posted to Jane's activity by Sara
await ericClient.addCommentReaction({
id: saraComment.id,
type: "like",
create_notification_activity: true,
});A few more cases:
- A reply to a comment notifies the parent comment's author, with the trigger type
comment_reply. - Mentions only create a notification when the post or comment is created with
create_notification_activity: true. When an edit adds a mention, sendhandle_mention_notifications: true. - Unliking, deleting a comment or unfollowing leaves the notification in place, unless the call sends
delete_notification_activity: true.
Reactions, follows and mentions from the same user on the same target are deduplicated, so a like, unlike and like again shows once. Comments and replies are not deduplicated. The full list of actions and their notification types is in Built-in notification feed.
3. Show the inbox and the badge
Read the user's notification feed. Each item in aggregated_activities is a group of similar notifications. The examples use notificationFeed, the user's notification feed, in the rest of this recipe.
// Read notifications
await notificationFeed.getOrCreate({
limit: 20,
});
console.log(notificationFeed.currentState.is_loading_activities);
console.log(notificationFeed.currentState.aggregated_activities);
await notificationFeed.getNextPage();Use the group's fields to write the line for each item:
| Field | Use it for |
|---|---|
activities |
The latest notifications in the group, up to the group size of 15. Take the name for "Jana" from an activity's user. |
user_count |
How many people acted among those activities. "Jana and 12 others" is the first name plus user_count - 1 others. |
user_count_truncated |
true when the group has more notifications than the group size. user_count then only counts the people in the latest ones, so use activity_count instead: "Jana and 40 others". |
activity_count |
The number of notifications in the group, including the ones past the group size. |
is_read, is_seen |
Whether to show the item as new. |
Each activity's notification_context holds the trigger (for example the comment that was added) and the target (the activity or comment it was added to). Use them to open the right screen when the user taps the item: the follower's profile for a follow, the post (target.id) for a reaction, comment or mention, and the comment in its post (target.comment.id) for a comment_reply or a reaction to a comment. See Trigger and Target.
For the badge on the bell icon, use unseen from the feed's notification_status. It counts groups, not single notifications, and stops at 100, so show "99+" past 99.
const status = notificationFeed.state.getLatestValue().notification_status;
const unread = status?.unread ?? 0;
const unseen = status?.unseen ?? 0;4. Mark notifications as seen and read
Most apps follow two rules:
- Seen: when the user opens the inbox, mark everything as seen. The badge clears.
- Read: when the user taps a group, mark that group as read. It stops looking new.
When the inbox opens:
await notificationFeed.markActivity({ mark_all_seen: true });When the user taps a group, pass its group value:
await notificationFeed.markActivity({ mark_read: [group.group] });5. Update the inbox in real time
On the client, read the notification feed with watch: true (add it to the getOrCreate call in step 3) to receive updates over WebSocket. Stream sends feeds.notification_feed.updated whenever the feed changes, with the updated groups and notification_status, and the client SDKs apply it to the feed's state, so the list and the badge update without reading the feed again. Don't poll the notification feed for new notifications, on the client or from your server: read it once when the app starts, and let the events keep it up to date. See Realtime events.
6. Send push notifications
Once push is set up for your app (see Push notifications), Stream sends a push for follows, comments, reactions and mentions. Two feed groups decide which actions send one:
- The feed the action happened in,
userby default. Itspush_typesare action types,follow,comment,reactionandcomment_reaction, and activity types such aspost. A mention in a post is pushed when the post's type is in the list. - The
notificationgroup, when a notification is added. Itspush_typesare matched against the notification's trigger type, which for a reaction is the reaction type, such aslike.
To stop a type of action from sending a push, remove it from both groups. See Feed group push configuration for examples, and Customizing notifications to change the text of each push.
Going further
- Group differently. Change the
notificationgroup's aggregation format, for example to group comment replies on the same comment. Send the group'snotificationandpush_notificationsettings in the same update, or they're reset. See Aggregating on comments and activities and Aggregation. - Add your own notification types. Add activities to a user's notification feed from your server, for example "Your order shipped". See Manual activity addition to notification feeds.
- Show every notification on its own. Turn off aggregation for a flat list. See Flat notification feeds.