Blazor 8 Push Notifications

48 views Asked by At

I'm trying to READ the notification in my component but nothing happens after the push. [A.Razor] call my HubConnection to SendAsync, it calls it BUt On my other componenet [MainLayout.razor] I don't get anything.

So far my A.razor :


private HubConnection? hubConnection;
public bool IsConnected =>
    hubConnection?.State == HubConnectionState.Connected;
protected override async Task OnInitializedAsync()
    {
        if (hubConnection is null)
        {
            hubConnection = new HubConnectionBuilder()
        .WithUrl(Navigation.ToAbsoluteUri("/chathub"))
        .Build();
            await hubConnection.StartAsync();
        }


        listCode = await CodeService.GetCodeAccesAsync();
    }

 private async Task AddLign()
    {
if (hubConnection is not null)
        {
            await hubConnection.SendAsync("SendMessage", "userInput", "messageInput");
        }
        await OnInitializedAsync();
}

My Hub :


public class SignalRHub : Hub
    {
        //appelle depuis le js
        public async Task SendMessage(string user, string msg)
        {
            await Clients.All.SendAsync("ReceiveMessage", user, msg, DateTime.Now.ToShortTimeString());
        }

        public async Task SendList()
        {
            await Clients.All.SendAsync("ReceiveMessage", new List<int>{ 1,2});
        }

    }

Then in my MainLayout.razor

private HubConnection? hubConnection;
protected override async Task OnInitializedAsync()
    {
        hubConnection = new HubConnectionBuilder()
           .WithUrl(Navigation.ToAbsoluteUri("/chathub"))
           .Build();
        

        hubConnection.On<string, string>("ReceiveMessage", (user, message) =>
        {
            string encodedMsg = $"{user}: {message}";
            AllNotificationMessages.Add(new NotificationModel("Mon Titre"));
            currentMessages = AllNotificationMessages.Where(elem => elem.Id != myMsg.Id).ToList();
            StateHasChanged();
        });
        await hubConnection.StartAsync();
    }

In Program.cs

builder.Services.AddResponseCompression(opts =>
{
    opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
          new[] { "application/octet-stream" });
});
//builder.Services.AddSignalRCore();
builder.Services.AddSignalR();



app.MapHub<SignalRHub>("/chatHub");`

Why I don't received notifications ? I was in a blazor server Ap p that I migrated to .NET 8.

1

There are 1 answers

3
Jason Pan On

We need to install Newtonsoft.Json package, make sure that string types are used uniformly when sending and receiving data.

Method in hub.

public async Task SendList()
{
    // Use JsonConvert.SerializeObject
    await Clients.All.SendAsync("ReceiveMessage", "ForListData", JsonConvert.SerializeObject(new List<int>{ 1,2}));
}

In your Page.

hubConnection.On<string, string>("ReceiveMessage", (user, message) =>
{
    if (type == "ForListData")
    {
        // Use JsonConvert.DeserializeObject
        var list = JsonConvert.DeserializeObject<List<int>>(message);
        //...
    }
    //...
    string encodedMsg = $"{user}: {message}";
    AllNotificationMessages.Add(new NotificationModel("Mon Titre"));
    currentMessages = AllNotificationMessages.Where(elem => elem.Id != myMsg.Id).ToList();
    StateHasChanged();
});