Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving push subscriptions

I am currently reading on developers.google.com about push notifications.

And I've come so far as to get a push subscription object.
But I am not sure about the best way to save it.

Here is the code from google

function subscribeUserToPush() {
    return getSWRegistration().then(function(registration) {
        const subscribeOptions = {
            userVisibleOnly: true,
            applicationServerKey: urlBase64ToUint8Array('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
        };

        return registration.pushManager.subscribe(subscribeOptions);
    }).then(function(pushSubscription) {
        console.log('Received PushSubscription: ', JSON.stringify(pushSubscription));
        return pushSubscription;
    });
}

I have a database, with a table of users, so should I create a new column there, and store the stringified JSON subscription object there?

Or better to create a new table for subscriptions?
For example a table like this?

user_id | endpoint | p256dh | auth

Can a user have multiple subscriptions? In that case a dedicated subscription table is preferred I think.

And about the VAPID keys, should I just put the public right into the code like above? And then store the private one somewhere safe?

Thank you.

like image 891
Ørnulf Arntsen Avatar asked Oct 27 '25 05:10

Ørnulf Arntsen


1 Answers

A user could have multiple subscriptions, if they are using your site via multiple devices (e.g. mobile and desktop).

Regarding VAPID, yes, your public key can be in the JavaScript client code. Your private key should only be known to you and so should be put somewhere safe on the server.

like image 140
Marco Castelluccio Avatar answered Oct 28 '25 19:10

Marco Castelluccio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!