English 中文(简体)
Enabling push notification with React.js and signalR
原标题:

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.

问题回答

暂无回答




相关问题
Port Number for Incoming Apple Push Notifications

What network services, port numbers, outgoing incoming or both, need to be open on a network for an ipod touch to receive Apple Push Notifications? I know the outgoing port number to set when sending ...

Push notifications dialogue

Is there any way I could make the "Would you like to enable push notifications..." dialogue not to come up upon first time application launch but rather on some other action in the application? ...

热门标签