Skip to content

Commit 2d7c9a2

Browse files
authored
[openhabcloud] Support hiding notifications and tags (openhab#16979)
* Support hiding notifications See openhab#16934 * Adds support for using tag over severity on extended actions Signed-off-by: Dan Cunningham <[email protected]>
1 parent 766981b commit 2d7c9a2

14 files changed

+611
-108
lines changed

bundles/org.openhab.io.openhabcloud/README.md

+30-14
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,19 @@ The parameters for these actions have the following meaning:
108108
- `emailAddress`: String containing the email address the target user is registered with in the cloud instance.
109109
- `message`: String containing the notification message text.
110110
- `icon`: String containing the icon name (as described in [Items: Icons]({{base}}/configuration/items.html#icons)).
111-
- `severity`: String containing a description of the severity of the incident.
111+
- `severity`: String containing a description of the severity (tag) of the incident.
112112

113113
`null` may be used to skip the `icon` or `severity` parameter.
114114

115-
### Title, Media Attachments & Actions
115+
### Title, Tag, Reference Id, Media Attachments & Actions
116116

117-
The `sendNotification` and `sendBroadcastNotification` actions additionally support setting a title, media attachments and actions.
117+
The `sendNotification` and `sendBroadcastNotification` actions additionally support setting a title, reference id, media attachments and actions.
118118

119-
The title is displayed as the notification title on the device and defaults to "openHAB" for the Android and iOS apps.
120-
Media attachments are displayed together with the notification on the device and can be used to display images, e.g. a camera snapshot.
121-
Actions allow the user to interact with the notification, e.g. to open a specific page in the app or to send a command to an Item.
119+
- The tag is used for tagging messages for grouping when displaying in the app and to hide/remove groups of messages from a user's device. Tag was previously referred to as "severity".
120+
- The title is displayed as the notification title on the device and defaults to "openHAB" for the Android and iOS apps.
121+
- The reference id is a user supplied identifier that when set will replace messages with the same id on the user's device (so only the last version exists). It can be used to update or remove notifications.
122+
- Media attachments are displayed together with the notification on the device and can be used to display images, e.g. a camera snapshot.
123+
- Actions allow the user to interact with the notification, e.g. to open a specific page in the app or to send a command to an Item.
122124

123125
There are four different actions available:
124126

@@ -127,14 +129,16 @@ There are four different actions available:
127129

128130
To specify media attachments and actions, there is another variant of the `sendNotification` and `sendBroadcastNotification` actions:
129131

130-
- `sendNotification(emailAddress, message, icon, severity, title, onClickAction, mediaAttachmentUrl, actionButton1, actionButton2, actionButton3)`
131-
- `sendBroadcastNotification(message, icon, severity, title, onClickAction, mediaAttachmentUrl, actionButton1, actionButton2, actionButton3)`
132+
- `sendNotification(emailAddress, message, icon, tag, title, referenceId, onClickAction, mediaAttachmentUrl, actionButton1, actionButton2, actionButton3)`
133+
- `sendBroadcastNotification(message, icon, tag, title, referenceId, onClickAction, mediaAttachmentUrl, actionButton1, actionButton2, actionButton3)`
132134

133-
The additional parameter for these variants have the following meaning:
134135

136+
The additional parameter for these variants have the following meaning:
137+
- `tag` : A user supplied tag to group messages for removing using the `hideNotificationByTag` action or grouping messages when displayed in the app. This renames the `severity` parameter, both are functionally identical.
135138
- `title`: The title of the notification. Defaults to "openHAB" inside the Android and iOS apps.
139+
- `referenceId`: A user supplied id to both replace existing messages when pushed, and later remove messages with the `hideNotificationByReferenceId` actions.
136140
- `onClickAction`: The action to be performed when the user clicks on the notification. Specified using the [action syntax](#action-syntax).
137-
- `mediaAttachmentUrl`: The URL of the media attachment to be displayed with the notification. This URL must be reachable by the push notification client.
141+
- `mediaAttachmentUrl`: The URL of the media attachment to be displayed with the notification. This can either be a fully qualified URL, prefixed with `http://` or `https://` and reachable by the client device, or an image item with the format `item:MyImageItem`
138142
- `actionButton1`: The action to be performed when the user clicks on the first action button. Specified as `Title=$action`, where `$action` follows the [action syntax](#action-syntax).
139143
- `actionButton2`: The action to be performed when the user clicks on the second action button. Specified as `Title=$action`, where `$action` follows the [action syntax](#action-syntax).
140144
- `actionButton3`: The action to be performed when the user clicks on the third action button. Specified as `Title=$action`, where `$action` follows the [action syntax](#action-syntax).
@@ -151,6 +155,7 @@ There are two types of actions available:
151155
- `ui`: Controls the UI in two possible ways:
152156
- `ui:$path` where `$path` is either `/basicui/app?...` for navigating sitemaps (using the native renderer) or `/some/absolute/path` for navigating (using the web view).
153157
- `ui:$commandItemSyntax` where `$commandItemSyntax` is the same syntax as used for the [UI Command Item]({{base}}/mainui/about.html#ui-command-item).
158+
- `http:` or `https:` : Opens the fully qualified URL in an embedded browser on the device
154159

155160
Examples:
156161

@@ -160,6 +165,16 @@ Examples:
160165
- `ui:/some/absolute/path`: Navigates to the absolut path `/some/absolute/path`.
161166
- `ui:navigate:/page/my_floorplan_page`: Navigates Main UI to the page with the ID `my_floorplan_page`.
162167
- `ui:popup:oh-clock-card`: Opens a popup with `oh-clock-card`.
168+
- `https://openhab.org`: Opens an embedded browser to the site `https://openhab.org`
169+
170+
### Hide Notification Actions
171+
172+
There are also actions to hide existing notifications, either by `referenceId` or `tag` (formerly severity):
173+
174+
- `hideNotificationByReferenceId(emailAddress, referenceId)`
175+
- `hideBroadcastNotificationByReferenceId(referenceId)`
176+
- `hideNotificationByTag(emailAddress, tag)`
177+
- `hideBroadcastNotificationByTag(tag)`
163178

164179
### Examples
165180

@@ -266,8 +281,8 @@ rule "Motion Detected Notification"
266281
when
267282
Item Apartment_MotionSensor changed to ON
268283
then
269-
sendBroadcastNotification("Motion detected in the apartment!", "motion", "MEDIUM",
270-
"Motion Detected", null, "https://apartment.my/camera-snapshot.jpg",
284+
sendBroadcastNotification("Motion detected in the apartment!", "motion", "Motion Tag",
285+
"Motion Detected", "motion-id-1234", null, "https://apartment.my/camera-snapshot.jpg",
271286
"Turn on the light=command:Apartment_Light:ON", null, null)
272287
end
273288
```
@@ -280,8 +295,9 @@ end
280295
rules.when().item('Apartment_MotionSensor').changed().to('ON').then(() => {
281296
actions.notificationBuilder('Motion detected in the apartment!')
282297
.withIcon('motion')
283-
.withSeverity('MEDIUM')
298+
.withTag('motion-tag')
284299
.withTitle('Motion Detected')
300+
.withReferenceId('motion-id-1234')
285301
.withMediaAttachment('https://apartment.my/camera-snapshot.jpg')
286302
.addActionButton('Turn on the light=command:Apartment_Light:ON')
287303
.send();
@@ -298,7 +314,7 @@ rule "Motion Detected Notification" do
298314
run do
299315
notify "Motion detected in the apartment!",
300316
icon: "motion",
301-
severity: "MEDIUM",
317+
tag: "motion-tag",
302318
title: "Motion Detected",
303319
attachment: "https://apartment.my/camera-snapshot.jpg",
304320
buttons: { "Turn on the light" => "command:Apartment_Light:ON" }

bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/NotificationAction.java

+81-24
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,12 @@ public static void sendNotification(String userId, String message) {
5252
* @param userId the cloud user id of the recipient
5353
* @param message the body of the notification
5454
* @param icon name for the notification
55-
* @param severity category for the notification
55+
* @param tag for the notification (formerly severity)
5656
*
5757
*/
5858
@ActionDoc(text = "Sends a push notification to mobile devices of user with userId")
59-
public static void sendNotification(String userId, String message, @Nullable String icon,
60-
@Nullable String severity) {
61-
sendNotification(userId, message, icon, severity, null, null, null, null, null, null);
59+
public static void sendNotification(String userId, String message, @Nullable String icon, @Nullable String tag) {
60+
sendNotification(userId, message, icon, tag, null, null, null, null, null, null, null);
6261
}
6362

6463
/**
@@ -67,8 +66,9 @@ public static void sendNotification(String userId, String message, @Nullable Str
6766
* @param userId the cloud user id of the recipient
6867
* @param message the body of the notification
6968
* @param icon name for the notification
70-
* @param severity category for the notification
69+
* @param tag for the notification
7170
* @param title for the notification
71+
* @param referenceId an identifier used to collapse and hide notifications
7272
* @param onClickAction the action to perform when clicked
7373
* @param mediaAttachmentUrl the media to attach to a notification
7474
* @param actionButton1 an action button in the format "Title=Action"
@@ -77,13 +77,14 @@ public static void sendNotification(String userId, String message, @Nullable Str
7777
*
7878
*/
7979
@ActionDoc(text = "Sends a push notification to mobile devices of user with userId")
80-
public static void sendNotification(String userId, String message, @Nullable String icon, @Nullable String severity,
81-
@Nullable String title, @Nullable String onClickAction, @Nullable String mediaAttachmentUrl,
82-
@Nullable String actionButton1, @Nullable String actionButton2, @Nullable String actionButton3) {
80+
public static void sendNotification(String userId, String message, @Nullable String icon, @Nullable String tag,
81+
@Nullable String title, @Nullable String referenceId, @Nullable String onClickAction,
82+
@Nullable String mediaAttachmentUrl, @Nullable String actionButton1, @Nullable String actionButton2,
83+
@Nullable String actionButton3) {
8384
logger.debug("sending notification '{}' to user {}", message, userId);
8485
if (cloudService != null) {
85-
cloudService.sendNotification(userId, message, icon, severity, title, onClickAction, mediaAttachmentUrl,
86-
actionButton1, actionButton2, actionButton3);
86+
cloudService.sendNotification(userId, message, icon, tag, title, referenceId, onClickAction,
87+
mediaAttachmentUrl, actionButton1, actionButton2, actionButton3);
8788
}
8889
}
8990

@@ -105,14 +106,14 @@ public static void sendLogNotification(String message) {
105106
*
106107
* @param message the body of the notification
107108
* @param icon name for the notification
108-
* @param severity category for the notification
109+
* @param tag for the notification (formerly severity)
109110
*
110111
*/
111112
@ActionDoc(text = "Sends a log notification which is shown in notifications log to all account users")
112-
public static void sendLogNotification(String message, @Nullable String icon, @Nullable String severity) {
113+
public static void sendLogNotification(String message, @Nullable String icon, @Nullable String tag) {
113114
logger.debug("sending log notification '{}'", message);
114115
if (cloudService != null) {
115-
cloudService.sendLogNotification(message, icon, severity);
116+
cloudService.sendLogNotification(message, icon, tag);
116117
}
117118
}
118119

@@ -134,12 +135,12 @@ public static void sendBroadcastNotification(String message) {
134135
*
135136
* @param message the body of the notification
136137
* @param icon name for the notification
137-
* @param severity category for the notification
138+
* @param tag for the notification (formerly severity)
138139
*
139140
*/
140-
@ActionDoc(text = "Sends a push notification to mobile devices of user with userId")
141-
public static void sendBroadcastNotification(String message, @Nullable String icon, @Nullable String severity) {
142-
sendBroadcastNotification(message, icon, severity, null, null, null, null, null, null);
141+
@ActionDoc(text = "Sends a broadcast notification to all mobile devices of all account users")
142+
public static void sendBroadcastNotification(String message, @Nullable String icon, @Nullable String tag) {
143+
sendBroadcastNotification(message, icon, tag, null, null, null, null, null, null, null);
143144
}
144145

145146
/**
@@ -148,23 +149,79 @@ public static void sendBroadcastNotification(String message, @Nullable String ic
148149
*
149150
* @param message the body of the notification
150151
* @param icon name for the notification
151-
* @param severity category for the notification
152+
* @param tag for the notification
152153
* @param title for the notification
154+
* @param referenceId an identifier used to collapse and hide notifications
153155
* @param onClickAction the action to perform when clicked
154156
* @param mediaAttachmentUrl the media to attach to a notification
155157
* @param actionButton1 an action button in the format "Title=Action"
156158
* @param actionButton2 an action button in the format "Title=Action"
157159
* @param actionButton3 an action button in the format "Title=Action"
158160
*
159161
*/
160-
@ActionDoc(text = "Sends a push notification to mobile devices of user with userId")
161-
public static void sendBroadcastNotification(String message, @Nullable String icon, @Nullable String severity,
162-
@Nullable String title, @Nullable String onClickAction, @Nullable String mediaAttachmentUrl,
163-
@Nullable String actionButton1, @Nullable String actionButton2, @Nullable String actionButton3) {
162+
@ActionDoc(text = "Sends a broadcast notification to all mobile devices of all account users")
163+
public static void sendBroadcastNotification(String message, @Nullable String icon, @Nullable String tag,
164+
@Nullable String title, @Nullable String referenceId, @Nullable String onClickAction,
165+
@Nullable String mediaAttachmentUrl, @Nullable String actionButton1, @Nullable String actionButton2,
166+
@Nullable String actionButton3) {
164167
logger.debug("sending broadcast notification '{}' to all users", message);
165168
if (cloudService != null) {
166-
cloudService.sendBroadcastNotification(message, icon, severity, title, onClickAction, mediaAttachmentUrl,
167-
actionButton1, actionButton2, actionButton3);
169+
cloudService.sendBroadcastNotification(message, icon, tag, title, referenceId, onClickAction,
170+
mediaAttachmentUrl, actionButton1, actionButton2, actionButton3);
171+
}
172+
}
173+
174+
/**
175+
* Hides notifications that contains a matching reference id to all mobile devices of a single user.
176+
*
177+
* @param userId the cloud user id of the recipient
178+
* @param referenceId the user reference id
179+
*
180+
*/
181+
@ActionDoc(text = "Hides notifications that contain the reference id on mobile devices of user with userId")
182+
public static void hideNotificationByReferenceId(String userId, String referenceId) {
183+
if (cloudService != null) {
184+
cloudService.hideNotificationByReferenceId(userId, referenceId);
185+
}
186+
}
187+
188+
/**
189+
* Hides notifications that contains a matching reference id to all mobile devices of all users of the account
190+
*
191+
* @param referenceId the user reference id
192+
*
193+
*/
194+
@ActionDoc(text = "Hides notifications that contain the reference id on all mobile devices of all account users")
195+
public static void hideBroadcastNotificationByReferenceId(String referenceId) {
196+
if (cloudService != null) {
197+
cloudService.hideBroadcastNotificationByReferenceId(referenceId);
198+
}
199+
}
200+
201+
/**
202+
* Hides notifications that are associated with a tag to all mobile devices of a single user.
203+
*
204+
* @param userId the cloud user id of the recipient
205+
* @param tag the tag associated with notifications
206+
*
207+
*/
208+
@ActionDoc(text = "Hides notifications that are associated with a tag on mobile devices of user with userId")
209+
public static void hideNotificationByTag(String userId, String tag) {
210+
if (cloudService != null) {
211+
cloudService.hideNotificationByTag(userId, tag);
212+
}
213+
}
214+
215+
/**
216+
* Hides notifications that are associated with a tag to all mobile devices of all users of the account
217+
*
218+
* @param tag the tag associated with notifications
219+
*
220+
*/
221+
@ActionDoc(text = "Hides notifications that are associated with a tag on all mobile devices of all account users")
222+
public static void hideBroadcastNotificationByTag(String tag) {
223+
if (cloudService != null) {
224+
cloudService.hideBroadcastNotificationByTag(tag);
168225
}
169226
}
170227
}

0 commit comments

Comments
 (0)