web-push-libs / web-push-csharp

Web Push library for C#
Mozilla Public License 2.0
429 stars 108 forks source link

SendNotification hangs #23

Closed zoharshavit closed 6 years ago

zoharshavit commented 6 years ago

Hi, Whenever my server executes the SendNotification method it hangs and not returning from it. The notification, however, is successfully recived on client's service worker. This is my code:

var vapidPrivateKey = "MI-X-.............x3w";
var vapidDetails = new VapidDetails("mailto:myemailaddress", "BErTgwJ.........", vapidPrivateKey);
var subscription = new PushSubscription(Endpoint, P256dh, Auth);
var webPushClient = new WebPushClient();
try
{
    var msg = new Msg() { title = "new message title", content = "new content, WOW!" };
    webPushClient.SendNotification(subscription,
            Newtonsoft.Json.JsonConvert.SerializeObject(msg, Newtonsoft.Json.Formatting.None),
            vapidDetails);
}
catch (WebPushException exception)
{
    Console.WriteLine("Http STATUS code" + exception.StatusCode);
}
catch (Exception e)
{
}

Any ideas? Since I do get the notification, I think my code is fine, but.... Thanks

zoharshavit commented 6 years ago

Anyone?

Ullvar commented 6 years ago

Dont know og you have solved this one, but it wont hang if you use the async method

zoharshavit commented 6 years ago

Thank you @Ullvar, I solved my issue by executing the send in a different thread. So now, my code looks like this: Thread t = new Thread((a) => { try { webPushClient.SendNotification((WebPush.PushSubscription)a, Newtonsoft.Json.JsonConvert.SerializeObject(msg, Newtonsoft.Json.Formatting.None)); } catch (WebPushException exception) { Console.WriteLine("Http STATUS code" + exception.StatusCode); } catch (Exception e) { } }); var s = new WebPush.PushSubscription(endpoint, p256dh, auth); t.Start(s); The thing I don't understand is why the sync method did not return even though it sent the msg.

Ullvar commented 6 years ago

Great! I dont know but it could be that beacuse javascript is async and scince this solution is ported from a js solution it fails beacuse c# thinks that it is a sync method.