I managed to run a working example for sending web-push notifications - subscribe user for push notifications, get the endpoint, generate the two browser
keys from the subscription object - p256dh
and auth
. On the server side I generate the VAPID
keys.
So, with all these I call sendNotification
on the web-push
nodejs package, and also - I'm passing a payload.
On Firefox - I get the notification with the payload.
On Chrome and Opera I get WebPushError: Received unexpected response code
and furthermore - UnauthorizedRegistration
and Error 400
.
The server side code I'm using to send the stuff is:
// import our web-push package ..
var webPush = require('web-push');
webPush.setGCMAPIKey('MY_GCM_SENDER_ID');
// we generated the VAPID keys ..
var vapidKeys = {
"publicKey": "...",
"privateKey": "..."
};
// set our VAPID credentials ..
webPush.setVapidDetails(
'mailto:{my email}',
vapidKeys.publicKey,
vapidKeys.privateKey
);
var device_endpoint = "https://android.googleapis.com/gcm/send/...";
var device_key = "...";
var device_auth = "...";
/*
* Sending the notification ..
*/
webPush.sendNotification(
{
endpoint: device_endpoint,
keys: {
p256dh: device_key,
auth: device_auth
}
},
'My payload',
{
TTL: 86400, // 24 hours ..
}
)
.then(function() {
console.log('SUCCESS');
})
.catch(function(err) {
console.log('Unsuccessful');
console.log(err);
});
I also have put MY_GCM_SENDER_ID as gcm_sender_id
in manifest.json
file; I took it from https://console.firebase.google.com/
- created a project and got the Sender ID
from Settings - Cloud Messaging
. Instructions for this I read here:
https://firebase.google.com/docs/cloud-messaging/js/client
So ... what am I doing wrong?