Register Visitors
Your website visitors need to be uniquely identified to receive push notifications.
Every visitor is assigned a unique device token that you can use to push it at any given time. Once the visitor has been assigned a device token, it should be stored in your application's backend database.
Web Push Registration
Add the following code to register the visitor's browser for push notifications.
Please make sure to populate the appId
variable with your Pushy App ID (Pushy Dashboard -> Click your app -> App Settings -> App ID):
// Add an import statement if you installed from npm
import Pushy from 'pushy-sdk-web';
// Register visitor's browser for push notifications
Pushy.register({ appId: 'YOUR_APP_ID' }).then(function (deviceToken) {
// Print device token to console
alert('Pushy device token: ' + deviceToken);
// Send the token to your backend server via an HTTP GET request
//fetch('https://your.api.hostname/register/device?token=' + deviceToken);
// Succeeded, optionally do something to alert the user
}).catch(function (err) {
// Notify user of failure
alert('Registration failed: ' + err.message);
});
Note: Please make sure to only call Pushy.register()
inside an onClick()
listener event handler of a link or button on your page, as some browsers will block registrations without a user-initiated event.
Additional note: Registration will fail if the user declines the Web Push permission dialog, in which case notification preferences for your hostname must be cleared in the browser settings for the dialog to be displayed again:
- Chrome: Enter
chrome://settings/content/notifications
in the address bar - Firefox: Enter
about:preferences#content
in the address bar (detailed instructions) - iOS 16.4+: Toggle "Allow Notifications" in the device settings for your PWA app, and restart the app