Search code examples
c#push-notificationpushservice-workerweb-push

Web push C# library function


I am currently working on web push notifications and am at the last stage of using the web push libraries to send the notification.

I am using the C# web push library here. However, I do not see a notification when on the page or when not on it.

I am attaching my code below:

I wrote the code in my store subscription method so it could be one of the issues.

 [HttpPost]
 public void StoreSubscription(string [] publicKey, string [] auth, string notificationEndPoint )
 {
            var pushEndpoint = notificationEndPoint;
            var pushAuth = auth[0].ToString();
            var pushP256DH = publicKey[0].ToString();

            var subject = "mailTo:[email protected]";
            var uPublicKey = "yyzzxx";
            var privateKey = "xxyyzz";

            System.IO.File.WriteAllText(@"\Desktop\Subscription.txt", pushEndpoint);
            var subscription = new PushSubscription(pushEndpoint, pushP256DH, pushAuth);
            var gcmAPIKey = "AAAA";
            var vapidDetails = new VapidDetails(subject, uPublicKey, privateKey);

            var webPushClient = new WebPushClient();

            try
            {
                webPushClient.SetGCMAPIKey(gcmAPIKey);
                webPushClient.SendNotification(subscription, "payload", gcmAPIKey);

            }
            catch (WebPushException exception)
            {
                Console.WriteLine("HTTP status Code:" + exception.StatusCode);
            }
}

And my service worker code is as follows for handling the push:

 self.addEventListener('push', function (event) {
    debugger
    var body;
    if (event.data) {
        body = event.data.text();
    } else {
        body = 'Push message no payload';
    }
    var options = {
        body: body,/*'This message was generated from a push'*/
        icon: '/Images/noun_Pushing_1823359.png',
        vibrate: [100, 200, 100, 200, 400],
        data: {
            dateOfArrival: Date.now(),
            primaryKey: '2'
        },
        actions: [
            {
                action: 'explore', title: 'Explore this new world',
                icon: '/Images/noun_Check Mark_4870.png'
            },
            {
                action: 'close', title: 'Close',
                icon: '/Images/noun_Close_887590.png'
            },
        ]
    };
    event.waitUntil(
        self.registration.showNotification('Push Notification', options)
    );
});

I have been stuck on this for almost a long time now and very new to promise, service worker and push and notification apis.

the function is getting hit and does not throw any exception. Also, when i put a debugger in the service worker, it does not get hit so apparently the push is not getting handles.I might be completely wrong on this.


Solution

  • I had the same issue. I base 64 encoded my auth and p256dh used to create the subscription.

    If there is no exception it could mean the JSON payload is valid JSON but in the wrong format for the service worker. I Changed my payload to a valid JSON object for my service worker (Angular):

    { "notification": {"title": "message title", "body": "message body"} }

    Please see docs... https://developer.mozilla.org/en-US/docs/Web/API/Notification/Notification

    Sent notification and hit the service worker.

    I was using Vapid Details not GCMApiKey.