|
1 |
| -# alert-notification |
| 1 | + |
| 2 | +The `@cap-js/notifications` package is a [CDS plugin](https://cap.cloud.sap/docs/node.js/cds-plugins#cds-plugin-packages) providing out-of-the box support for publishing business notifications. |
| 3 | + |
| 4 | +### Table of Contents |
| 5 | + |
| 6 | +- [Setup](#setup) |
| 7 | +- [Usage](#usage) |
| 8 | + - [Update Notification Configuration](#update-notification-configuration) |
| 9 | + - [Notification Destination](#notification-destination) |
| 10 | + - [Notification Types Path](#notification-types-path) |
| 11 | + - [Notification Type Prefix](#notification-type-prefix) |
| 12 | + - [Add Notification Types](#add-notification-types) |
| 13 | + - [Update handlers to publish notification](#update-handlers-to-publish-notification) |
| 14 | + - [Simple Notificaiton with title](#simple-notificaiton-with-title) |
| 15 | + - [Simple Notificaiton with title & description](#simple-notificaiton-with-title) |
| 16 | + - [Custom Notifications with notification types](#simple-notificaiton-with-title) |
| 17 | + - [Sample Application with notifications](#sample-application-with-notifications) |
| 18 | + - [In Local Environment](#in-local-environment) |
| 19 | + - [In Production Environment](#in-production-environment) |
| 20 | +- [Contributing](#contributing) |
| 21 | + - [Code of Conduct](#code-of-conduct) |
| 22 | +- [Licensing](#licensing) |
| 23 | + |
| 24 | +## Setup |
| 25 | + |
| 26 | +To enable notifications, simply add this self-configuring plugin package to your project: |
| 27 | + |
| 28 | +```sh |
| 29 | + cds add notifications |
| 30 | +``` |
| 31 | + |
| 32 | +<img width="1300" alt="cds add notifications" style="border-radius:0.5rem" src="_assets/cdsAddNotifications.gif"> |
| 33 | + |
| 34 | +## Usage |
| 35 | + |
| 36 | +In this guide, we use the [Incidents Management reference sample app](https://github.com/cap-js/incidents-app) as the base, to publish notifications. |
| 37 | + |
| 38 | +### Update Notification Configuration |
| 39 | + |
| 40 | +`cds add notifications` will add default configurations for notifications in the `package.json` file. |
| 41 | + |
| 42 | +<img width="1300" alt="Default Notification config" style="border-radius:0.5rem" src="_assets/packageJsonConfig.gif"> |
| 43 | + |
| 44 | +#### **Notification Destination** |
| 45 | + |
| 46 | +As a pre-requisite to publish the notification, you need to have a [destination](https://help.sap.com/docs/build-work-zone-standard-edition/sap-build-work-zone-standard-edition/enabling-notifications-for-custom-apps-on-sap-btp-cloud-foundry#configure-the-destination-to-the-notifications-service) configured to publish the notification. In the `package.json` by default destination name `SAP_Notification` is added, you can modify the destination name that you are configuring. |
| 47 | + |
| 48 | +#### **Notification Types Path** |
| 49 | + |
| 50 | +When you run `cds add notifications`, it will add `notificationstype.json` file with template for a notification type in the project root folder. You can add the notification types in the `notificationtype.json` file for sending the custom notification types. |
| 51 | + |
| 52 | +#### **Notification Type Prefix** |
| 53 | + |
| 54 | +To make notification types unique to the application, prefix is added to the type key. By default, `application name` is added as the prefix. You can update the `prefix` if required. |
| 55 | + |
| 56 | +### Add Notification Types |
| 57 | + |
| 58 | +If you want to send custom notifications in your application, you can add the notification types in the `notificationtype.json` file. |
| 59 | + |
| 60 | +Sample: If you want to send the notification when the new incident is reported, you can modify the `notificationtypes.json` as below: |
| 61 | + |
| 62 | +```jsonc |
| 63 | +[ |
| 64 | + { |
| 65 | + "NotificationTypeKey": "IncidentReported", |
| 66 | + "NotificationTypeVersion": "1", |
| 67 | + "Templates": [ |
| 68 | + { |
| 69 | + "Language": "en", |
| 70 | + "TemplatePublic": "Incident Reported", |
| 71 | + "TemplateSensitive": "Incident '{{name}}' Reported", |
| 72 | + "TemplateGrouped": "New Incidents", |
| 73 | + "TemplateLanguage": "mustache", |
| 74 | + "Subtitle": "Incident '{{name}}' reported by '{{customer}}'." |
| 75 | + } |
| 76 | + ] |
| 77 | + } |
| 78 | +] |
| 79 | +``` |
| 80 | + |
| 81 | +### Update handlers to publish notification |
| 82 | + |
| 83 | +In the handler files, connect to the notifications plugin by: |
| 84 | + |
| 85 | +```js |
| 86 | +const alert = await cds.connect.to('notifications'); |
| 87 | +``` |
| 88 | + |
| 89 | +#### **Simple Notificaiton with title** |
| 90 | +You can use the following signature to send the simple notification with title |
| 91 | +```js |
| 92 | +alert.notify({ |
| 93 | + recipients: recipients, |
| 94 | + priority: priority, |
| 95 | + title: title |
| 96 | +}); |
| 97 | +``` |
| 98 | +#### **Simple Notificaiton with title & description** |
| 99 | +You can use the following signature to send the simple notification with title and description |
| 100 | +```js |
| 101 | +alert.notify({ |
| 102 | + recipients: recipients, |
| 103 | + priority: priority, |
| 104 | + title: title, |
| 105 | + description: description |
| 106 | +}); |
| 107 | +``` |
| 108 | +#### **Custom Notifications with notification types** |
| 109 | +You can use the following signature to send the custom notification with pre-defined notification types. |
| 110 | +```js |
| 111 | +alert.notify({ |
| 112 | + NotificationTypeKey: 'IncidentCreated', |
| 113 | + NotificationTypeVersion: '1', |
| 114 | + Priority: 'NEUTRAL', |
| 115 | + Properties: [ |
| 116 | + { |
| 117 | + Key: 'name', |
| 118 | + IsSensitive: false, |
| 119 | + Language: 'en', |
| 120 | + Value: 'Engine overheating', |
| 121 | + Type: 'String' |
| 122 | + }, |
| 123 | + { |
| 124 | + Key: 'customer', |
| 125 | + IsSensitive: false, |
| 126 | + Language: 'en', |
| 127 | + Value: 'John', |
| 128 | + Type: 'String' |
| 129 | + } |
| 130 | + ], |
| 131 | + Recipients: recipients |
| 132 | +}); |
| 133 | +``` |
| 134 | + |
| 135 | +### Sample Application with notifications |
| 136 | + |
| 137 | +#### **In Local Environment** |
| 138 | +In local environment, when you publish notification, it is mocked to publish the nofication to the console. |
| 139 | + |
| 140 | +<img width="1300" alt="Notify to console" style="border-radius:0.5rem;padding:1rem;background:rgb(24 24 24)" src="_assets/notifyToConsole.png"> |
| 141 | + |
| 142 | +#### **In Production Environment** |
| 143 | + |
| 144 | +Once application is deployed and integrated with SAP Build Work Zone, you can see the notification under fiori notifications icon! |
| 145 | + |
| 146 | +<img width="1300" alt="Sample Application Demo" style="border-radius:0.5rem;" src="_assets/incidentsNotificationDemo.gif"> |
| 147 | + |
| 148 | +## Contributing |
| 149 | + |
| 150 | +This project is open to feature requests/suggestions, bug reports etc. via [GitHub issues](https://github.com/cap-js/change-tracking/issues). Contribution and feedback are encouraged and always welcome. For more information about how to contribute, the project structure, as well as additional contribution information, see our [Contribution Guidelines](CONTRIBUTING.md). |
| 151 | + |
| 152 | + |
| 153 | +### Code of Conduct |
| 154 | + |
| 155 | +We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone. By participating in this project, you agree to abide by its [Code of Conduct](CODE_OF_CONDUCT.md) at all times. |
| 156 | + |
| 157 | + |
| 158 | +## Licensing |
| 159 | + |
| 160 | +Copyright 2023 SAP SE or an SAP affiliate company and contributors. Please see our [LICENSE](LICENSE) for copyright and license information. Detailed information including third-party components and their licensing/copyright information is available [via the REUSE tool](https://api.reuse.software/info/github.com/cap-js/change-tracking). |
| 161 | + |
| 162 | + |
2 | 163 |
|
0 commit comments