---
title: "How to send notification internally?"  
description: "How to send notification internally?"  
author: "ICSM Computer"  
published: 2025-06-18  
updated: 2025-06-18  
canonical: https://www.mindstick.com/interview/34255/how-to-send-notification-internally  
category: "c#"  
tags: ["c#", "signalr"]  
reading_time: 4 minutes  

---

# How to send notification internally?

To **send internal (in-app) notifications using SignalR**, you can broadcast or send messages to specific users or groups **without page reloads**.

Here’s a full breakdown of how to implement **internal notifications** in an ASP.NET Core or ASP.NET MVC + SignalR app:

## 1. Define a Hub

### C# (NotificationHub.cs)

```cs
public class NotificationHub : Hub
{
    public async Task SendNotificationToUser(string userId, string message)
    {
        await Clients.User(userId).SendAsync("ReceiveNotification", message);
    }

    public async Task BroadcastNotification(string message)
    {
        await Clients.All.SendAsync("ReceiveNotification", message);
    }
}
```

> You can also use `Clients.Group(...)`, `Clients.Caller`, etc., based on your use case.

## 2. Configure SignalR in Startup

### ASP.NET Core:

In `Startup.cs`:

```cs
public void ConfigureServices(IServiceCollection services)
{
    services.AddSignalR();

    // Optional: Add authentication
    services.AddAuthentication(...);
}

public void Configure(IApplicationBuilder app)
{
    app.UseRouting();

    app.UseAuthentication();
    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapHub<NotificationHub>("/notificationHub");
    });
}
```

## ✅ 3. Client-Side JavaScript

### HTML + JS:

```html
<script src="https://cdnjs.cloudflare.com/ajax/libs/microsoft-signalr/7.0.5/signalr.min.js"></script>
<script>
    const connection = new signalR.HubConnectionBuilder()
        .withUrl("/notificationHub")
        .build();

    connection.on("ReceiveNotification", function (message) {
        alert("Notification: " + message); // or show in your UI
    });

    connection.start().catch(function (err) {
        return console.error(err.toString());
    });
</script>
```

## 4. Send Notifications from Backend

### Example in a Controller:

```cs
public class NotificationController : Controller
{
    private readonly IHubContext<NotificationHub> _hub;

    public NotificationController(IHubContext<NotificationHub> hub)
    {
        _hub = hub;
    }

    public async Task<IActionResult> NotifyUser(string userId, string message)
    {
        await _hub.Clients.User(userId).SendAsync("ReceiveNotification", message);
        return Ok();
    }

    public async Task<IActionResult> NotifyAll(string message)
    {
        await _hub.Clients.All.SendAsync("ReceiveNotification", message);
        return Ok();
    }
}
```

## 5. Make Sure Authentication is Set Up for Targeted Users

For `Clients.User(userId)` to work:

- The user must be authenticated.
- SignalR maps the `userId` from `ClaimsPrincipal.NameIdentifier`.

> If using ASP.NET MVC (non-Core), SignalR setup is slightly different. Let me know if you're on MVC 5.

## Optional Enhancements

| Feature | Description |
| --- | --- |
| Notification queue | Store unread messages in DB |
| User groups | Send based on roles or teams |
| Toaster UI | Use libraries like toastr, noty, or custom popups |
| Badge counters | Show unread notification counts live |

## Answers

### Answer by ICSM Computer

To **send internal (in-app) notifications using SignalR**, you can broadcast or send messages to specific users or groups **without page reloads**.

Here’s a full breakdown of how to implement **internal notifications** in an ASP.NET Core or ASP.NET MVC + SignalR app:

## 1. Define a Hub

### C# (NotificationHub.cs)

```cs
public class NotificationHub : Hub
{
    public async Task SendNotificationToUser(string userId, string message)
    {
        await Clients.User(userId).SendAsync("ReceiveNotification", message);
    }

    public async Task BroadcastNotification(string message)
    {
        await Clients.All.SendAsync("ReceiveNotification", message);
    }
}
```

> You can also use `Clients.Group(...)`, `Clients.Caller`, etc., based on your use case.

## 2. Configure SignalR in Startup

### ASP.NET Core:

In `Startup.cs`:

```cs
public void ConfigureServices(IServiceCollection services)
{
    services.AddSignalR();

    // Optional: Add authentication
    services.AddAuthentication(...);
}

public void Configure(IApplicationBuilder app)
{
    app.UseRouting();

    app.UseAuthentication();
    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapHub<NotificationHub>("/notificationHub");
    });
}
```

## ✅ 3. Client-Side JavaScript

### HTML + JS:

```html
<script src="https://cdnjs.cloudflare.com/ajax/libs/microsoft-signalr/7.0.5/signalr.min.js"></script>
<script>
    const connection = new signalR.HubConnectionBuilder()
        .withUrl("/notificationHub")
        .build();

    connection.on("ReceiveNotification", function (message) {
        alert("Notification: " + message); // or show in your UI
    });

    connection.start().catch(function (err) {
        return console.error(err.toString());
    });
</script>
```

## 4. Send Notifications from Backend

### Example in a Controller:

```cs
public class NotificationController : Controller
{
    private readonly IHubContext<NotificationHub> _hub;

    public NotificationController(IHubContext<NotificationHub> hub)
    {
        _hub = hub;
    }

    public async Task<IActionResult> NotifyUser(string userId, string message)
    {
        await _hub.Clients.User(userId).SendAsync("ReceiveNotification", message);
        return Ok();
    }

    public async Task<IActionResult> NotifyAll(string message)
    {
        await _hub.Clients.All.SendAsync("ReceiveNotification", message);
        return Ok();
    }
}
```

## 5. Make Sure Authentication is Set Up for Targeted Users

For `Clients.User(userId)` to work:

- The user must be authenticated.
- SignalR maps the `userId` from `ClaimsPrincipal.NameIdentifier`.

> If using ASP.NET MVC (non-Core), SignalR setup is slightly different. Let me know if you're on MVC 5.

## Optional Enhancements

| Feature | Description |
| --- | --- |
| Notification queue | Store unread messages in DB |
| User groups | Send based on roles or teams |
| Toaster UI | Use libraries like toastr, noty, or custom popups |
| Badge counters | Show unread notification counts live |


---

Original Source: https://www.mindstick.com/interview/34255/how-to-send-notification-internally

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
