# 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.

>
> **Info:** 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](https://getstream.io/docs/platform/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).

```kotlin label="Kotlin"
// Eric follows Jane
ericFeed.follow(
    targetFid = janeFeed.fid,
    createNotificationActivity = true
)

// Eric comments on Jane's activity
ericFeed.addComment(
    ActivityAddCommentRequest(
        comment = "Agree!",
        activityId = janeActivity.id,
        createNotificationActivity = true
    )
)

// Eric reacts to Jane's activity
ericFeed.addActivityReaction(
    activityId = janeActivity.id,
    request = AddReactionRequest(
        type = "like",
        createNotificationActivity = true
    ),
)

// Eric reacts to a comment posted to Jane's activity by Sara
ericFeed.addCommentReaction(
    commentId = saraComment.id,
    request = AddCommentReactionRequest(
        type = "like",
        createNotificationActivity = true
    ),
)
```

```java label="Java"
// Eric follows Jane
feeds.follow(FollowRequest.builder()
    .source("timeline:eric")
    .target("user:jane")
    .createNotificationActivity(true)
    .build()).execute();

// Eric comments on Jane's activity
feeds.addComment(AddCommentRequest.builder()
    .comment("Agree!")
    .objectID("<jane's activity id>")
    .objectType("activity")
    .createNotificationActivity(true)
    .userID("eric")
    .build()).execute();

// Eric reacts to Jane's activity
feeds.addActivityReaction("<jane's activity id>", AddActivityReactionRequest.builder()
    .type("like")
    .createNotificationActivity(true)
    .userID("eric")
    .build()).execute();

// Eric reacts to a comment posted to Jane's activity by Sara
feeds.addCommentReaction("<sara's comment id>", AddCommentReactionRequest.builder()
    .type("like")
    .createNotificationActivity(true)
    .userID("eric")
    .build()).execute();
```

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, send `handle_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](https://getstream.io/activity-feeds/docs/android/notification-feeds/#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.

```kotlin label="Kotlin"
val notificationFeed = client.feed(group = "notification", id = "jane")
val notifications = notificationFeed.getOrCreate()
```

```java label="Java"
Feed notificationFeed = new Feed("notification", "jane", feeds);

// Read notifications
List<AggregatedActivityResponse> notifications = notificationFeed.getOrCreate(
    GetOrCreateFeedRequest.builder()
        .limit(20)
        .userID("jane")
        .build()).getData().getAggregatedActivities();
```

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](https://getstream.io/activity-feeds/docs/android/notification-feeds/#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.

```js label="JavaScript"
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:

```kotlin label="Kotlin"
notificationFeed.markActivity(request = MarkActivityRequest(markAllSeen = true))
```

```java label="Java"
notificationFeed.markActivity(MarkActivityRequest.builder()
    .markAllSeen(true)
    .userID("jane")
    .build()).execute();
```

When the user taps a group, pass its `group` value:

```kotlin label="Kotlin"
notificationFeed.markActivity(request = MarkActivityRequest(markRead = listOf(group.group)))
```

```java label="Java"
notificationFeed.markActivity(MarkActivityRequest.builder()
    .markRead(List.of(group.getGroup()))
    .userID("jane")
    .build()).execute();
```

## 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](https://getstream.io/activity-feeds/docs/android/notification-feeds/#realtime-events).

## 6. Send push notifications

Once push is set up for your app (see [Push notifications](https://getstream.io/docs/platform/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, `user` by default. Its `push_types` are action types, `follow`, `comment`, `reaction` and `comment_reaction`, and activity types such as `post`. A mention in a post is pushed when the post's type is in the list.
- The `notification` group, when a notification is added. Its `push_types` are matched against the notification's trigger type, which for a reaction is the reaction type, such as `like`.

To stop a type of action from sending a push, remove it from both groups. See [Feed group push configuration](https://getstream.io/activity-feeds/docs/node/feed-group-push-configuration/) for examples, and [Customizing notifications](https://getstream.io/activity-feeds/docs/node/push-template/) to change the text of each push.

## Going further

- **Group differently.** Change the `notification` group's aggregation format, for example to group comment replies on the same comment. Send the group's `notification` and `push_notification` settings in the same update, or they're reset. See [Aggregating on comments and activities](https://getstream.io/activity-feeds/docs/android/notification-feeds/#aggregating-on-comments-and-activities) and [Aggregation](https://getstream.io/activity-feeds/docs/android/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](https://getstream.io/activity-feeds/docs/android/notification-feeds/#manual-activity-addition-to-notification-feeds).
- **Show every notification on its own.** Turn off aggregation for a flat list. See [Flat notification feeds](https://getstream.io/activity-feeds/docs/android/notification-feeds/#flat-notification-feeds).

---

For the most recent version of this documentation, visit [https://getstream.io/activity-feeds/docs/android/notification-inbox-recipe/](https://getstream.io/activity-feeds/docs/android/notification-inbox-recipe/).