Legacy Dev Forum Posts

 View Only

Sign Up

[NEW FEATURE] NotificationHandler Helper Class in v0.73.9.356

  • 1.  [NEW FEATURE] NotificationHandler Helper Class in v0.73.9.356

    Posted 06-05-2025 18:04

    tim.smith | 2016-08-17 21:07:10 UTC | #1

    As of v0.73.9.356, the .NET SDK contains a set of classes that can be used to handle notifications. Full documentation for the classes can be found in the readme.

    In order to use these classes, you must manually reference the WebSocketSharp package. This can be accomplished with the nuget command: Install-Package WebSocketSharp -Pre. This is not included as a nuget dependency due to a nuget limitation that prevents prerelease packages from being included as dependencies. This manual requirement will be removed in a future release.

    TLDR;

    Example app: https://github.com/T-Boss/purecloud_platform_api_test_apps/tree/master/csharp/console%20app

    Code:

    var handler = new NotificationHandler(); handler.AddSubscription($"v2.users.{me.Id}.presence", typeof(UserPresenceNotification)); handler.AddSubscription($"v2.users.{me.Id}.conversations", typeof(ConversationNotification)); handler.NotificationReceived += (data) => { Console.WriteLine(JsonConvert.SerializeObject(data, Formatting.Indented));

    if (data.GetType() == typeof (NotificationData<UserPresenceNotification>)) { var presence = (NotificationData<UserPresenceNotification>) data; Console.WriteLine($"New presence: {presence.EventBody.PresenceDefinition.SystemPresence}"); } else if (data.GetType() == typeof (NotificationData<ConversationNotification>)) { var conversation = (NotificationData<ConversationNotification>) data; Console.WriteLine($"Conversation: {conversation.EventBody.Id}"); } };


    vrvoice1 | 2016-08-18 09:33:06 UTC | #2

    Hi Tim,

    thats realy nice. But I have the same DateTime issue like this: https://developer.mypurecloud.com/forum/t/participant-model-unrecognizedpropertyexception-on-wrapupexpected/270/1 in the NotificationHandler.

    <img src="//inin-prod-use1-developerforum.s3.amazonaws.com/original/1X/74e0b5b47918d9f00f607f4bce9beab07b0828eb.PNG" width="690" height="139">

    Perhaps it's possible to add a "RemoveAllSubscriptions" method ;-)

    Regards, Sven


    tim.smith | 2016-08-18 13:13:24 UTC | #3

    I knew I was forgetting something... I was going to mention there is a bug with the notification schema where it's specifying an int when it should be a datetime. This is being fixed but the change hasn't hit prod yet. If you see this error with any notification other than the conversations topic, please let me know.

    I will definitely add a RemoveAllSubscriptions method.


    tim.smith | 2016-08-18 14:23:44 UTC | #4

    NotificationHandler.RemoveAllSubscriptions() as of 0.77.9.360


    canderson | 2016-08-25 20:40:16 UTC | #5

    This is excellent! Just yesterday I had a client ask about something regarding notifications so the timing is perfect for me!

    One thing I would like to suggest is passing through a couple more of the events from the WebSocket object.

    Obviously "OnMessage" is already getting passed through in the "NotificationReceived" event. But has there been any thought about passing through the "OnError" or "OnClose"? It would be nice to be able to detect when the web socket closes and immediately try to reconnect to continue receiving notifications.


    tim.smith | 2016-08-25 21:08:44 UTC | #6

    You can interact with the websocket directly via the NotificationHandler.WebSocket property. If you'd like to add the helper methods/events, I'd accept a PR for that.


    skansakar | 2016-08-26 15:31:54 UTC | #7

    @tim.smith Is this feature going to be available for Java SDK too?


    tim.smith | 2016-08-26 16:18:20 UTC | #8

    Yes it's on the list of features to add.


    system | 2017-08-28 19:26:31 UTC | #10


    This post was migrated from the old Developer Forum.

    ref: 303