Your plugin can send its own notifications. They work just like the notifications of RISE:
- Settings: they are listed in Settings > Notifications, under your plugin's category. The admin turns on email, web or Slack there and chooses who gets them. Don't build your own notification settings page.
- Web and push: they show in the notification bell and are sent as push notifications.
- Email: they are sent with the general notification email template, or with your own template.
- Slack: they are sent to Slack when Slack is on for the event.
You don't need any SQL or table changes. RISE adds the setting row by itself the first time the event runs, or when the admin opens Settings > Notifications.
Call all the register functions below from your plugin's index.php file.
Step 1: Register an event
register_notification_event("my_plugin_document_approved", array(
"category" => "my_plugin_documents",
"notify_to" => array("team_members", "team", "client_primary_contact", "my_plugin_document_owner"),
"info" => function ($notification, $plugin_data) {
return array("url" => get_uri("my_plugin/view/" . get_array_value($plugin_data, "document_id")));
},
"description" => function ($notification, $plugin_data, $for_slack) {
$title = get_array_value($plugin_data, "title");
return $for_slack ? "\n*Document:* " . $title : "Document: " . esc($title) . "";
},
"can_notify_user" => function ($user, $context) {
return true; //return false to skip this user
},
"email_template" => "my_plugin_document_approved",
"email_parser_data" => function ($notification, $plugin_data) {
return array("DOCUMENT_TITLE" => get_array_value($plugin_data, "title"));
},
"defaults" => array(
"enable_web" => 1,
"enable_email" => 1,
"enable_slack" => 0,
"notify_to_terms" => array("my_plugin_document_owner")
)
));
| Option | Required | What it does |
| category | Yes | Language key of the category. It is shown in the category filter of the notification settings page. |
| notify_to | Yes | Who the admin can choose to notify. RISE terms: team_members, team, project_members, client_primary_contact, client_all_contacts, client_assigned_contacts, task_assignee, task_collaborators, ticket_creator, ticket_assignee, leave_applicant, recipient, mentioned_members, owner, post_creator, order_creator_contact, estimate_creator, proposal_creator. You can also add your own terms (see step 2). A RISE term finds users only when you pass its id when you send the notification. For example, project_members needs project_id. |
| info | No | A function that tells where a click on the notification goes. Return array("url" => ...) to open a page, array("ajax_modal_url" => ..., "id" => ..., "large_modal" => "1") to open a modal, or array("app_modal_url" => ...). Without it, the notification can't be clicked. |
| description | No | A function that returns extra details. They are shown under the notification text and in the {EVENT_DETAILS} part of the email. When $for_slack is true, return plain text. Always escape what users typed. |
| can_notify_user | No | A function that returns false to skip a user. Use it for your plugin's own permission rules. If the function throws an error, that user is skipped. |
| email_template | No | The name of a template added with register_email_template(). Without it, RISE uses the general notification template. |
| email_parser_data | No | A function that returns array("VARIABLE" => "value") for the email template. Variable names must be in capital letters (A-Z, 0-9 and _). Values must be text or numbers. You can't replace the RISE variables, like EVENT_TITLE, SIGNATURE or NOTIFICATION_URL. |
| defaults | No | The first settings of the event: enable_web, enable_email, enable_slack and notify_to_terms (a list of terms that are also in notify_to). After that, the admin changes them in Settings > Notifications. |
Rules for event names:
- Use only small letters, numbers and underscores. Start with a letter. Use 100 characters or less.
- Start the name with your plugin name, for example my_plugin_...
- You can't replace an event of RISE. If you try, your event is ignored and an error is written to the log.
Step 2: Add your own "notify to" option (optional)
Use this when your plugin decides who gets the notification, for example the owner or the reviewers of a document.
register_notification_term("my_plugin_document_owner", function ($context) {
$plugin_data = get_array_value($context, "plugin_data");
return array(get_array_value($plugin_data, "owner_id")); //return user ids
});
- $context has: event, user_id (the user who sent the notification), options (the ids you passed to log_notification) and plugin_data.
- Return a list of user ids. RISE still does its normal checks: only active users get it, each user's own notification choices are respected, and the user who sent the notification doesn't get it.
- The term runs only when the admin has selected it for the event, and only if it is in the event's notify_to list.
- The term is shown as an option on the settings page. Its label comes from the language key with the same name.
- Term names follow the same rules as event names, with 50 characters or less. You can't reuse a term name of RISE.
Step 3: Send the notification
log_notification("my_plugin_document_approved", array(
"client_id" => $client_id, //optional RISE ids
"plugin_data" => array(
"document_id" => $document_id,
"owner_id" => $owner_id,
"title" => $document_title
)
));
- plugin_data can be any array, up to 10 KB as JSON. RISE saves it with the notification and gives it to your info, description, email_parser_data and term functions.
- To read it anywhere else, use get_notification_plugin_data($notification). This function is in the notifications helper, so call helper('notifications') first if it isn't loaded.
- The third parameter of log_notification() sets who sent the notification. Leave it empty for the logged-in user, or pass "0" (as text) for a system notification.
- Only registered events are sent. If your plugin is turned off, its notifications are not sent.
Step 4: Add the language keys
Add these to your plugin's Language/
$lang["my_plugin_document_approved"] = "Document approved"; //event name in the settings list $lang["notification_my_plugin_document_approved"] = "approved a document."; //notification text $lang["my_plugin_documents"] = "Documents"; //category $lang["my_plugin_document_owner"] = "Document owner"; //your own term
Older hooks
The older hooks still work, but use the register functions above for new plugins:
- app_filter_notification_config
- app_filter_create_notification_where_query
- app_filter_notification_description
- app_filter_notification_description_for_slack
- app_filter_notification_category_suggestion
- app_filter_send_email_notification
- app_filter_email_templates
- app_hook_post_notification
- plugin_* options and columns in log_notification()