3

How to send notification using Webpush library. I was trying but its throws error msg like {"Received unexpected response code"}

****Now i have created web API to send notification & calling you through fiddler,but did n't get exception it's stuck somewhere here is my code sample****

public void Sendnotification()
        {
            try
            {
       WebPush.VapidDetails vapidKeys = apidHelper.GenerateVapidKeys();
         string subject =@"mailto:xyz.com";
         string publicKey = Convert.ToString(vapidKeys.PublicKey);
         string privateKey = Convert.ToString(vapidKeys.PrivateKey);

        var subscription = new PushSubscription(pushEndpoint, p256dh, auth);
        var vapidDetails = new VapidDetails(subject, publicKey, privateKey);

        client.SendNotification(subscription, "payload", vapidDetails);
            }
            catch (WebPushException e)
            {

            }
        }

I have configured Https enabled to call api using fidder. Please have look. also its throws error, it stuck somewhere enter image description here

now it got the error please have look it's showing error HTTP/1.1 410 NotRegistered enter image description here

See the full screen of Fiddler response error details

  • Use a sniffer like wireshark or fiddler to get more details on the error. If you can perform same request using a browser than compare browser results with your application. Most errors like this are caused by missing html header(s) in your request. – jdweng Apr 17 '17 at 11:03
  • please update your question with plain text and not images, btw I'm getting the same error wich is 410, not registered as a response – pedrommuller Apr 18 '17 at 01:29
  • I am using command line for testing purpose to send notification on web application using WebPush library. can you please tell me what is procedure for that. if you provide me some sample then it will be very good – Vineet Kumar Rai Apr 18 '17 at 10:19
  • @VineetKumarRai what's the exception you are getting in the webPushException? is the inner exception null? can you post your fiddler trace? – pedrommuller Apr 18 '17 at 13:36
  • its not showing exception in catch, it stuck somewherer, also i have enabled https to call api through fiddler – Vineet Kumar Rai Apr 18 '17 at 14:31

1 Answers1

1

If you are getting the error 410 (to check the error use fiddler to intercept the https call), probably what you have is an error in the subscription data of the user probably the keys stored in your database doesn't match the subscription in the browser an easy fix could be to re-subscribe and re-save the subscription data and try again.

to setup fiddler, you have to use it as a proxy visual studio to intercept the https calls and also you have to enable https decryption.

EDIT

you can set up fiddler just by adding this configuration in your web.config or app.config:

<system.net>
 <defaultProxy
  enabled = "true"
  useDefaultCredentials = "true">
 <proxy autoDetect="false" bypassonlocal="false" proxyaddress="http://127.0.0.1:8888" usesystemdefault="false" />
 </defaultProxy>
 </system.net>

if in any case, you get unauthorized registration check this questions:

Web Push API Chrome, returning "Unauthorized Registration"

WebPushError and UnauthorizedRegistration when try to send push notification to Chrome and Opera, FF is OK

Community
  • 1
  • 1
pedrommuller
  • 15,741
  • 10
  • 76
  • 126
  • Please lemme know about how to send notification on web application using webpush library – Vineet Kumar Rai Apr 18 '17 at 10:27
  • now i have create Web API for sending notificaton & called using fiddler but it was stuck somewhere didn't through any exception in this – Vineet Kumar Rai Apr 18 '17 at 11:18
  • just intercept the https calls with fiddler, use it as a proxy in visual studio, check my update – pedrommuller Apr 18 '17 at 13:23
  • I am using web push library. if you please explain in details then it will be very help ful https://github.com/web-push-libs/web-push-csharp – Vineet Kumar Rai Apr 18 '17 at 15:05
  • You don't have to code anything, just configure fiddle as a proxy in visual studio, to catch the real exception, as I see you are not intercepting the https calls that the web push library is doing to the google endpoints, once you get that error response then you can start to debug the "real error" which could be very straight forward – pedrommuller Apr 18 '17 at 15:17
  • remove the try catch and try to run the process again, just for debugging purposes – pedrommuller Apr 18 '17 at 15:18
  • I have done the same. now its showing error HTTP/1.1 410 NotRegistered see the screen shots above – Vineet Kumar Rai Apr 19 '17 at 07:55
  • click on number 13 then raw to see the full response – pedrommuller Apr 19 '17 at 13:53
  • btw have you registered your browser client notifications with your localhost server? – pedrommuller Apr 19 '17 at 13:59
  • Thankyou for replying & solving step by step which i was totall stuck, i am almost reach on to send notificaton. this error is related to UnauthorizedRegistration ..Click on the link(https://i.stack.imgur.com/WndUx.png) to See the full screen of Fiddler response error details. How to registered my browser client notification with localhost server. actually i am new in this i can't get it scenario that how i need to send notification – Vineet Kumar Rai Apr 19 '17 at 14:12
  • yes I knew that getting into the real error was the key point to solve debug and resolve – pedrommuller Apr 19 '17 at 14:19