Zalarox
Zalarox

Reputation: 168

Web push 201 but can't see notification on device

I followed Google's guide to send push notifications on Chrome. I set up a server on Node to send push notifications. On actually sending the push notification by making a POST request to my server's endpoint, I get a 201 indicating everything was okay, but I don't see the actual notification on my browser.

webpush.sendNotification(subscriptions[0], "{'notification': {'title': 'my title','body': 'my body',}}", {TTL: 60})
    .catch((err) => {
        console.log(err);
        res.json({ message: 'an error occurred' });
    })
    .then(function(success) {
        res.json({ message: 'sent a push...' });
        console.log('sent push successfully.');
        console.log(success);
    });

This is the response I get:

statusCode: 201,
body: '',
headers: {
    'content-type': 'text/plain',
    location: 'https://fcm.googleapis.com/fcm/...',
    date: 'Thu, 02 Nov 2017 12:31:10 GMT',
    expires: 'Thu, 02 Nov 2017 12:31:10 GMT',
    'cache-control': 'private, max-age=0',
    'x-content-type-options': 'nosniff',
    'x-frame-options': 'SAMEORIGIN',
    'x-xss-protection': '1; mode=block',
    'content-length': '0',
    server: 'GSE',
    'alt-svc': 'quic=":443"; ma=2592000; v="41,39,38,37,35"',
    connection: 'close'
}

If the user isn't subscribed, or the authorization key is off, I get a 400 error - so I know everything else should (apparently) be in order. What is wrong?

Upvotes: 3

Views: 2850

Answers (1)

Zalarox
Zalarox

Reputation: 168

Got it working! I tried out the same stuff in Firefox with my service worker code (make sure you are calling showNotification properly!) and everything worked great.

self.addEventListener('push', function(event) {

    var myNotif = event.data.json();
    const promiseChain = self.registration.showNotification(myNotif.title);

    event.waitUntil(promiseChain);
});

Any changes to service worker files are propagated after reopening the page. Finicky, but nothing was out of the ordinary in the code. Working on both Chrome and Firefox like a charm.

Upvotes: 1

Related Questions