2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.io.openhabcloud;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.core.model.script.engine.action.ActionDoc;
18 import org.openhab.io.openhabcloud.internal.CloudService;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * This class provides static methods that can be used in automation rules
24 * for sending notifications to the native apps.
26 * @author Victor Belov - Initial contribution
27 * @author Kai Kreuzer - migrated code to ESH APIs
28 * @author Dan Cunningham - Extended notification enhancements
31 public class NotificationAction {
33 private static final Logger logger = LoggerFactory.getLogger(NotificationAction.class);
35 public static @Nullable CloudService cloudService;
38 * Sends a simple push notification to mobile devices of user
40 * @param userId the cloud user id of the recipient
41 * @param message the body of the notification
44 @ActionDoc(text = "Sends a push notification to mobile devices of user with userId")
45 public static void sendNotification(String userId, String message) {
46 sendNotification(userId, message, null, null);
50 * Sends an advanced push notification to mobile devices of user
52 * @param userId the cloud user id of the recipient
53 * @param message the body of the notification
54 * @param icon name for the notification
55 * @param tag for the notification (formerly severity)
58 @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, @Nullable String tag) {
60 sendNotification(userId, message, icon, tag, null, null, null, null, null, null, null);
64 * Sends an advanced push notification to mobile devices of user
66 * @param userId the cloud user id of the recipient
67 * @param message the body of the notification
68 * @param icon name for the notification
69 * @param tag for the notification
70 * @param title for the notification
71 * @param referenceId an identifier used to collapse and hide notifications
72 * @param onClickAction the action to perform when clicked
73 * @param mediaAttachmentUrl the media to attach to a notification
74 * @param actionButton1 an action button in the format "Title=Action"
75 * @param actionButton2 an action button in the format "Title=Action"
76 * @param actionButton3 an action button in the format "Title=Action"
79 @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 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) {
84 logger.debug("sending notification '{}' to user {}", message, userId);
85 if (cloudService != null) {
86 cloudService.sendNotification(userId, message, icon, tag, title, referenceId, onClickAction,
87 mediaAttachmentUrl, actionButton1, actionButton2, actionButton3);
92 * Sends a simple notification to log. Log notifications are not pushed to user
93 * devices but are shown to all account users in notifications log.
95 * @param message the body of the notification
98 @ActionDoc(text = "Sends a log notification which is shown in notifications log to all account users")
99 public static void sendLogNotification(String message) {
100 sendLogNotification(message, null, null);
104 * Sends an advanced notification to log. Log notifications are not pushed to user
105 * devices but are shown to all account users in notifications log.
107 * @param message the body of the notification
108 * @param icon name for the notification
109 * @param tag for the notification (formerly severity)
112 @ActionDoc(text = "Sends a log notification which is shown in notifications log to all account users")
113 public static void sendLogNotification(String message, @Nullable String icon, @Nullable String tag) {
114 logger.debug("sending log notification '{}'", message);
115 if (cloudService != null) {
116 cloudService.sendLogNotification(message, icon, tag);
121 * Sends a simple broadcast notification. Broadcast notifications are pushed to all
122 * mobile devices of all users of the account
124 * @param message the body of the notification
127 @ActionDoc(text = "Sends a broadcast notification to all mobile devices of all account users")
128 public static void sendBroadcastNotification(String message) {
129 sendBroadcastNotification(message, null, null);
133 * Sends an advanced broadcast notification. Broadcast notifications are pushed to all
134 * mobile devices of all users of the account
136 * @param message the body of the notification
137 * @param icon name for the notification
138 * @param tag for the notification (formerly severity)
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);
147 * Sends an advanced broadcast notification. Broadcast notifications are pushed to all
148 * mobile devices of all users of the account
150 * @param message the body of the notification
151 * @param icon name for the notification
152 * @param tag for the notification
153 * @param title for the notification
154 * @param referenceId an identifier used to collapse and hide notifications
155 * @param onClickAction the action to perform when clicked
156 * @param mediaAttachmentUrl the media to attach to a notification
157 * @param actionButton1 an action button in the format "Title=Action"
158 * @param actionButton2 an action button in the format "Title=Action"
159 * @param actionButton3 an action button in the format "Title=Action"
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) {
167 logger.debug("sending broadcast notification '{}' to all users", message);
168 if (cloudService != null) {
169 cloudService.sendBroadcastNotification(message, icon, tag, title, referenceId, onClickAction,
170 mediaAttachmentUrl, actionButton1, actionButton2, actionButton3);
175 * Hides notifications that contains a matching reference id to all mobile devices of a single user.
177 * @param userId the cloud user id of the recipient
178 * @param referenceId the user reference id
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);
189 * Hides notifications that contains a matching reference id to all mobile devices of all users of the account
191 * @param referenceId the user reference id
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);
202 * Hides notifications that are associated with a tag to all mobile devices of a single user.
204 * @param userId the cloud user id of the recipient
205 * @param tag the tag associated with notifications
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);
216 * Hides notifications that are associated with a tag to all mobile devices of all users of the account
218 * @param tag the tag associated with notifications
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);