SimplePushNotification
This class contains methods for creating and sending push notifications from the SimpleOne system to user browsers.
Specify recipients
To send a notification, you must specify at least one recipient using the setRecipientUserIds() or setRecipientGroupIds() method. If a specified user belongs to one or more specified groups, the system creates a single recipient record for that user. A single notification can have no more than 1000 unique recipients.
setRecipientUserIds(userIds)
Use this method to set push notification recipients with user IDs. Repeated calls overwrite the value.
Parameter(s):
| Name | Type | Mandatory | Default value |
|---|---|---|---|
| userIds | Array of Strings | Y | N |
Return:
| Type | Description |
|---|---|
| Boolean | Returns true if the value is accepted. Returns false if the value is invalid. |
Example:
let webpush = new SimplePushNotification();
webpush.setRecipientUserIds(['155931135900000001','155931135900000032']);
setRecipientGroupIds(groupIds)
Use this method to set push notification recipients with group IDs. Repeated calls overwrite the value.
Parameter(s):
| Name | Type | Mandatory | Default value |
|---|---|---|---|
| groupIds | Array of Strings | Y | N |
Return:
| Type | Description |
|---|---|
| Boolean | Returns true if the value is accepted. Returns false if the value is invalid. |
Example:
let webpush = new SimplePushNotification();
webpush.setRecipientGroupIds(['177068186608977300']);
Specify notification contents and behavior
The setTitle(title) and setBody(body) methods are mandatory when creating a push notification. They must not be Empty or Null.
setTitle(title)
Use this method to set the push notification title. The title length must not exceed 35 characters. If the value exceeds 35 characters, it is truncated.
Parameter(s):
| Name | Type | Mandatory | Default value |
|---|---|---|---|
| title | String | Y | N |
Return:
| Type | Description |
|---|---|
| Boolean | Returns true if the value is accepted without modification. Returns false if the value is missing, has an invalid type, or was truncated to 35 characters. |
Example:
let webpush = new SimplePushNotification();
webpush.setTitle('Title Text');
setBody(body)
Use this method to set the push notification body text. The body length must not exceed 255 characters. If the value exceeds 255 characters, it is truncated.
Parameter(s):
| Name | Type | Mandatory | Default value |
|---|---|---|---|
| body | String | Y | N |
Return:
| Type | Description |
|---|---|
| Boolean | Returns true if the value is accepted without modification. Returns false if the value is missing, has an invalid type, or was truncated to 255 characters. |
Example:
let webpush = new SimplePushNotification();
webpush.setBody('Notification body text');
setIconUrl(iconUrl)
Use this method to display an icon in the push notification, the URL of which is passed in the iconUrl parameter. The default value is set by the push_notifications.default_icon_url system property.
Parameter(s):
| Name | Type | Mandatory | Default value |
|---|---|---|---|
| iconUrl | String | Y | https://simpleone.ru/favicon/favicon.ico |
Return:
| Type | Description |
|---|---|
| Boolean | Returns false if an invalid iconUrl value is set. |
Example:
let webpush = new SimplePushNotification();
webpush.setIconUrl('https://simpleone.ru/favicon/favicon.ico')
setUrl(linkUrl)
Use this method to set the URL for navigation when a user clicks the push notification.
Parameter(s):
| Name | Type | Mandatory | Default value |
|---|---|---|---|
| linkUrl | String | Y | N |
Return:
| Type | Description |
|---|---|
| Boolean | Returns false if an invalid linkUrl value is set. |
Example:
let webpush = new SimplePushNotification();
webpush.setUrl('https://your-instance.simpleone.ru/');
setUrgency(severity)
Use this method to set the urgency level of the push notification.
If the value is not set or is invalid, the value of the push_notifications.default_urgency system property is used. If this property does not exist, is not set, or is set incorrectly, the value set in the column properties of the Urgency column of the Push Notification (sys_push_notification) table is used.
Parameter(s):
| Name | Type | Mandatory | Valid values | Default value |
|---|---|---|---|---|
| severity | String | Y |
| normal |
Return:
| Type | Description |
|---|---|
| Boolean | Returns false if an invalid severity value is set. |
Example:
let webpush = new SimplePushNotification();
webpush.setUrgency('normal');
setTtl(ttl)
Use this method to set the time to live of the push notification in seconds. Maximum value: 2419200.
If the value is invalid (not an integer from 0 to 2419200) or not set, the value of the push_notifications.default_ttl system property is used. If this property does not exist, is not set, or is set incorrectly, the value set in the column properties of the Time to live column of the Push Notification (sys_push_notification) table is used.
Parameter(s):
| Name | Type | Mandatory | Valid values | Default value |
|---|---|---|---|---|
| ttl | Integer | Y | From 0 to 2419200 | 86400 |
Return:
| Type | Description |
|---|---|
| Boolean | Returns false if an invalid ttl value is set. |
Example:
let webpush = new SimplePushNotification();
webpush.setTtl(86400);
setInteraction(interaction)
Use this method to set whether a click on the push notification is required to dismiss it.
Parameter(s):
| Name | Type | Mandatory | Valid values | Default value |
|---|---|---|---|---|
| interaction | Boolean | Y |
| N |
Return:
| Type | Description |
|---|---|
| Boolean | Returns false if an invalid interaction value is set. |
Example:
let webpush = new SimplePushNotification();
webpush.setInteraction(true);
Retrieve and validate data
getJson()
Use this method to get the current values of the SimplePushNotification class instance.
This method does not accept any parameters.
Return:
| Type | Description |
|---|---|
| String | A serialized JSON string containing the current field values of the class instance. |
Example:
const webpush = new SimplePushNotification();
webpush.setTitle('Request processed');
webpush.setBody('Your request has been processed');
ss.info(webpush.getJson());
validate()
Use this method to validate the current values of the SimplePushNotification instance without sending the notification.
This method does not accept any parameters.
Return:
| Type | Description |
|---|---|
| String | A serialized JSON string containing the validation results of the class instance fields. |
The result string may contain the following messages:
| Message | Fields | Condition |
|---|---|---|
The parameter does not contain a value | title, body | A mandatory value is missing. |
Some values are incorrect | userIds, groupIds | The array contains incorrect identifiers. |
The parameter contains an invalid value | url, ttl, iconUrl, urgency, interaction | The value does not match the type, format, or valid range. |
The parameter value has been truncated | title, body | The value has been truncated to the maximum allowed length. |
userID or groupID must be completed | recipients | No valid recipient users or groups specified. |
Example:
const webpush = new SimplePushNotification();
webpush.setTitle('Request processed');
webpush.setBody('Your request has been processed');
ss.info(webpush.validate());
Send notification
send()
Use this method for the logical sending of a push notification.
Before logical sending, the system validates the parameters. If mandatory parameters are not set or are set incorrectly, the following occurs:
- logical sending is not performed,
- a corresponding record is created in the System Log (sys_log).
Return:
| Type | Description |
|---|---|
| String | If logical sending is successful, returns the ID (sys_id) of the created record from the Push Notification (sys_push_notification) table. Otherwise, returns false. |
Example
let webpush = new SimplePushNotification();
webpush.setTitle('Request processed');
webpush.setBody('Your request has been processed');
webpush.setIconUrl('https://simpleone.ru/favicon/favicon.ico')
webpush.setUrl('https://your-instance.simpleone.ru/');
webpush.setInteraction(true);
webpush.setUrgency('normal');
webpush.setTtl(86400);
webpush.setRecipientUserIds(['155931135900000001']);
webpush.setRecipientGroupIds(['177068186608977300']);
ss.info(webpush.getJson());
const validate = webpush.validate();
ss.info(validate);
const id = webpush.send()
ss.info(id);