Web push notifications

Contents...

Your app or plugin can send web push notifications to users to inform them about various events such as new orders, phone calls, completed tasks, etc.

Web push notifications are sent to a user on the same device; e.g., PC, smartphone or tablet, on which they have expressed consent to receive them. A request for such consent is automatically made by tools available in the Webasyst framework.

For web push notifications to be sent, a user must set up connection to any provider in section Settings → Web push notifications.

How to use web push notifications in Webasyst

1. Add an event handler

Create a handler for the webasyst.backend_push event. In the handler’s code, return the true value to enable web push notifications in a Webasyst account.

When such a handler is available, all backend users are requested for consent to receive web push notifications.

If you would like to request such consent only on certain pages; e.g., only within your app, then return value true only in the case when a user has opened an appropriate page, and return false otherwise.

Once a user has confirmed their consent to receive notifications, a token linked to that user is automatically added to database table wa_push_subscribers to be used for sending notifications.

2. Send notifications

To send web push notifications, use an instance of class waPushAdapter, which must be obtained by calling wa()->getPush().

Before the actual sending use isEnabled() method to check whether the connection to a provider is set up in section Settings → Web push notifications. Return value false if it is disabled, which means that a notifications cannot be sent in this case.

Example
// obtain an instance of the notifications class
$push = wa()->getPush();

// check whether connection to a provider is set up in the system settings
if (!$push->isEnabled()) {
    return false;
}

// IDs of users to whom you need to send a particular notification
// you should obtain their list according to your app’s or plugin’s business logic
$contact_ids = [1, 2, 3, 10];

// complete an array of notification data
// each item of the array is optional
$data = [
    'title'     => '...', // notification title
    'message'   => '...', // notification text
    'url'       => 'https://...', // URL which will open if a user clicks on the notification
    'image_url' => 'https://...', // URL of an icon to be displayed in a notification
];

// send the notification to specified users
$push->sendByContact($contact_ids, $data);