Handle Notifications

Any notifications you send to your users are displayed in a browser push banner:

You can customize the notification title, message body, and image displayed, as well as the URL that will open when the notification is tapped by sending the following data payload to the Send Notifications API:

    "title": "Test Notification", // Notification title
    "url": "https://pushy.me/", // Opens when tapped
    "message": "Hello World!", // Notification body text
    "image": "https://example.com/image.png" // Optional image

In case your web page is open, you can define a listener that will be invoked to execute any relevant action in response to the notification being received.

Optionally add the following code to handle notifications, after Pushy.register():

// Handle push notifications (only when web page is open)
Pushy.setNotificationListener(function (data) {
    // Print notification payload data
    console.log('Received notification: ' + JSON.stringify(data));

    // Attempt to extract the "message" property from the payload: {"message":"Hello World!"}
    let message = data.message || 'Test notification';

    // Display an alert with message sent from server
    alert('Received notification: ' + message);

Parse Notification Data

Any payload data that you send with your push notifications is made available to your web page via the data parameter of the setNotificationListener callback.

If you were to send a push notification with the following data payload:

{"id": 1, "message": "Hello World!"}

Then you'd be able to retrieve these values in your setNotificationListener callback like so:

let id = data.id;
let message = data.message;