Skip to content

Commit f4ba4ca

Browse files
committed
readme
1 parent 6720566 commit f4ba4ca

File tree

1 file changed

+92
-78
lines changed

1 file changed

+92
-78
lines changed

README.md

Lines changed: 92 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,18 @@ The `@cap-js/notifications` package is a [CDS plugin](https://cap.cloud.sap/docs
77
### Table of Contents
88

99
- [Setup](#setup)
10-
- [Add code to send notifications](#add-code-to-send-notifications)
11-
- [Simple Notification with title](#simple-notification-with-title)
12-
- [Simple Notification with title and description](#simple-notification-with-title-and-description)
13-
- [Add Notification Types](#add-notification-types)
14-
- [Custom Notifications](#custom-notifications)
15-
- [With standard parameters](#with-standard-parameters)
16-
- [Passing the whole notification object](#passing-the-whole-notification-object)
17-
- [Sample Application with notifications](#sample-application-with-notifications)
18-
- [In Local Environment](#in-local-environment)
19-
- [In Production Environment](#in-production-environment)
20-
- [Notification Destination](#notification-destination)
21-
- [Integrate with SAP Build Work Zone](#integrate-with-sap-build-work-zone)
10+
- [Send Notifications](#add-code-to-send-notifications)
11+
- [Use Notification Types](#use-notification-types)
12+
- [API Reference](#api-reference)
13+
- [Test-drive Locally](#test-drive-locally)
14+
- [Run in Production](#run-in-production)
2215
- [Advanced Usage](#advanced-usage)
23-
- [Update Notification Configuration](#update-notification-configuration)
24-
- [Notification Types Path](#notification-types-path)
25-
- [Notification Type Prefix](#notification-type-prefix)
2616
- [Contributing](#contributing)
2717
- [Code of Conduct](#code-of-conduct)
2818
- [Licensing](#licensing)
2919

20+
21+
3022
## Setup
3123

3224
To enable notifications, simply add this self-configuring plugin package to your project:
@@ -35,25 +27,34 @@ To enable notifications, simply add this self-configuring plugin package to your
3527
npm add @cap-js/notifications
3628
```
3729

38-
## Usage
39-
4030
In this guide, we use the [Incidents Management reference sample app](https://github.com/cap-js/incidents-app) as the base, to publish notifications.
4131

42-
### Update Notification Configuration
4332

44-
`cds add notifications` will add default configurations for notifications in the `package.json` file.
4533

34+
## Send Notifications
4635

36+
With that you can use the NotificationService as any other CAP Service like so in you event handlers:
4737

48-
#### Notification Types Path
38+
```js
39+
const alert = await cds.connect.to('notifications');
40+
```
4941

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.
42+
You can use the following signature to send the simple notification with title and description
5143

52-
#### Notification Type Prefix
44+
```js
45+
alert.notify({
46+
recipients: [ ...supporters() ],
47+
priority: "HIGH",
48+
title: "New high priority incident is assigned to you!",
49+
description: "Incident titled 'Engine overheating' created by 'customer X' with priority high is assigned to you!"
50+
});
51+
```
5352

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.
5553

56-
### Add Notification Types
54+
55+
## Use Notification Types
56+
57+
### 1. Add notification types
5758

5859
If you want to send custom notifications in your application, you can add the notification types in the `notificationtype.json` file.
5960

@@ -62,56 +63,89 @@ Sample: If you want to send the notification when the new incident is reported,
6263
```jsonc
6364
[
6465
{
65-
"NotificationTypeKey": "IncidentReported",
66+
"NotificationTypeKey": "IncidentResolved",
6667
"NotificationTypeVersion": "1",
6768
"Templates": [
6869
{
6970
"Language": "en",
70-
"TemplatePublic": "Incident Reported",
71-
"TemplateSensitive": "Incident '{{name}}' Reported",
72-
"TemplateGrouped": "New Incidents",
71+
"TemplatePublic": "Incident Resolved",
72+
"TemplateSensitive": "Incident '{{title}}' Resolved",
73+
"TemplateGrouped": "Incident Status Update",
7374
"TemplateLanguage": "mustache",
74-
"Subtitle": "Incident '{{name}}' reported by '{{customer}}'."
75+
"Subtitle": "Incident from '{{customer}}' resolved by {{user}}."
7576
}
7677
]
7778
}
7879
]
7980
```
8081

81-
### Add code to send notifications
82+
### 2. Use pre-defined types in your code like that:
83+
8284

83-
In the handler files, connect to the notifications plugin by:
84-
85-
```js
86-
const alert = await cds.connect.to('notifications');
87-
```
8885

89-
#### Simple Notification with title
90-
You can use the following signature to send the simple notification with title
91-
```js
92-
alert.notify({
93-
94-
priority: "HIGH",
95-
title: "New incident is reported!"
96-
});
97-
```
98-
#### Simple Notification with title and description
99-
You can use the following signature to send the simple notification with title and description
10086
```js
101-
alert.notify({
102-
recipients: ["[email protected]"],
103-
priority: "HIGH",
104-
title: "New high priority incident is assigned to you!",
105-
description: "Incident titled 'Engine overheating' created by 'customer X' with priority high is assigned to you!"
106-
});
87+
await alert.notify ('IncidentResolved', {
88+
recipients: [ customer.id ],
89+
data: {
90+
customer: customer.info,
91+
title: incident.title,
92+
user: cds.context.user.id,
93+
}
94+
})
10795
```
10896

109-
#### Custom Notifications
97+
98+
99+
## API Reference
100+
101+
102+
103+
* **recipients** - List of the recipients, this argument is mandatory
104+
* **type** - Notification type key, this argument is mandatory
105+
* **priority** - Priority of the notification, this argument is optional, it defaults to NEUTRAL
106+
* **data** - A key-value pair that is used to fill a placeholder of the notification type template, this argument is optional
107+
108+
109+
110+
## Test-drive Locally
111+
In local environment, when you publish notification, it is mocked to publish the nofication to the console.
112+
113+
<img width="1300" alt="Notify to console" style="border-radius:0.5rem;padding:1rem;background:rgb(24 24 24)" src="_assets/notifyToConsole.png">
114+
115+
## Run in Production
116+
117+
#### Notification Destination
118+
119+
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.
120+
121+
#### Integrate with SAP Build Work Zone
122+
123+
Once application is deployed and [integrated with SAP Build Work Zone](https://github.com/cap-js/calesi/tree/main/samples/notifications), you can see the notification under fiori notifications icon!
124+
125+
<img width="1300" alt="Sample Application Demo" style="border-radius:0.5rem;" src="_assets/incidentsNotificationDemo.gif">
126+
127+
128+
129+
## Advanced Usage
130+
131+
132+
133+
#### Custom Notification Types Path
134+
135+
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.
136+
137+
#### Custom Notification Type Prefix
138+
139+
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.
140+
141+
#### Low-level Notifications API
142+
110143
You can use these two signature to send the custom notification with pre-defined notification types.
111144

112145
##### With standard parameters
113146

114147
By using this approach you can post a notification by providing different parts of the notification object grouped in related units
148+
115149
```js
116150
alert.notify({
117151
@@ -157,17 +191,14 @@ alert.notify({
157191
});
158192
```
159193

160-
Possible parameters:
161-
* **recipients** - List of the recipients, this argument is mandatory
162-
* **type** - Notification type key, this argument is mandatory
163-
* **priority** - Priority of the notification, this argument is optional, it defaults to NEUTRAL
164-
* **properties** - A key-value pair that is used to fill a placeholder of the notification type template, this argument is optional
165-
* **navigation** - All navigation related parameters, this argument is optional
166-
* **payload** - The rest parameters that can be passed, this argument is optional
194+
167195

168196
##### Passing the whole notification object
169197

170198
By using this approach you need to pass the whole notification object as described in the API documentation
199+
200+
201+
171202
```js
172203
alert.notify({
173204
NotificationTypeKey: 'IncidentCreated',
@@ -193,31 +224,14 @@ alert.notify({
193224
});
194225
```
195226

196-
### Sample Application with notifications
197-
198-
#### In Local Environment
199-
In local environment, when you publish notification, it is mocked to publish the nofication to the console.
200-
201-
<img width="1300" alt="Notify to console" style="border-radius:0.5rem;padding:1rem;background:rgb(24 24 24)" src="_assets/notifyToConsole.png">
202227

203-
#### In Production Environment
204-
205-
##### Notification Destination
206-
207-
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.
208-
209-
##### Integrate with SAP Build Work Zone
210-
211-
Once application is deployed and [integrated with SAP Build Work Zone](https://github.com/cap-js/calesi/tree/main/samples/notifications), you can see the notification under fiori notifications icon!
212-
213-
<img width="1300" alt="Sample Application Demo" style="border-radius:0.5rem;" src="_assets/incidentsNotificationDemo.gif">
214228

215229
## Contributing
216230

217231
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).
218232

219233

220-
### Code of Conduct
234+
## Code of Conduct
221235

222236
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.
223237

0 commit comments

Comments
 (0)