I need to enable pushnotifications using signalR
import { HubConnectionBuilder} from @microsoft/signalr ;
import jwt_decode from "jwt-decode";
let connection = null;
export function setupSignalRConnection(onNotificationReceived ) {
debugger
const token = localStorage.getItem("token");
let userId = "";
if (token) {
const decodedToken = jwt_decode(token);
userId = decodedToken.UserId;
console.log(userId);
}
connection = new HubConnectionBuilder()
.withUrl(( http://123.456.789.365/ExampleURL/NotificationUserHub?userId=${userId} ),
{
// skipNegotiation: true,
// transport: signalR.HttpTransportType.WebSockets,
timeout: 3000000,
queryString: `userId=${userId}`,
})
.withAutomaticReconnect()
.build();
connection.start()
.then(() => {
const connectionId = connection.connectionId;
console.log( Connected to SignalR hub. Connection ID: ,connectionId);
localStorage.setItem( signalrConnection ,JSON.stringify(connection));
// Perform any necessary actions after connection establishment
})
.catch(error => console.error(error));
connection.on("sendToParent", (notification) => {
console.log( Received notification: , notification);
onNotificationReceived(notification);
});
connection.on("sendToEmployee", (notification) => {
console.log( Send notification: , notification);
onNotificationReceived(notification);
});
return connection;
}
Now i am successfully getting connected, but i am not receiving notifications when some action is taken place.Is this code correct or shall i add something extra to make it work correctly, please suggest if this code have any fixes.