]> git.basschouten.com Git - openhab-addons.git/blob
2d4591dabe70a867a717aa0ad4e75a7ccdf344d6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.io.openhabcloud;
14
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;
21
22 /**
23  * This class provides static methods that can be used in automation rules
24  * for sending notifications to the native apps.
25  *
26  * @author Victor Belov - Initial contribution
27  * @author Kai Kreuzer - migrated code to ESH APIs
28  * @author Dan Cunningham - Extended notification enhancements
29  */
30 @NonNullByDefault
31 public class NotificationAction {
32
33     private static final Logger logger = LoggerFactory.getLogger(NotificationAction.class);
34
35     public static @Nullable CloudService cloudService;
36
37     /**
38      * Sends a simple push notification to mobile devices of user
39      *
40      * @param userId the cloud user id of the recipient
41      * @param message the body of the notification
42      *
43      */
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);
47     }
48
49     /**
50      * Sends an advanced push notification to mobile devices of user
51      *
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 severity category for the notification
56      *
57      */
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,
60             @Nullable String severity) {
61         sendNotification(userId, message, icon, severity, null, null, null, null, null, null);
62     }
63
64     /**
65      * Sends an advanced push notification to mobile devices of user
66      *
67      * @param userId the cloud user id of the recipient
68      * @param message the body of the notification
69      * @param icon name for the notification
70      * @param severity category for the notification
71      * @param title for the notification
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"
77      *
78      */
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 severity,
81             @Nullable String title, @Nullable String onClickAction, @Nullable String mediaAttachmentUrl,
82             @Nullable String actionButton1, @Nullable String actionButton2, @Nullable String actionButton3) {
83         logger.debug("sending notification '{}' to user {}", message, userId);
84         if (cloudService != null) {
85             cloudService.sendNotification(userId, message, icon, severity, title, onClickAction, mediaAttachmentUrl,
86                     actionButton1, actionButton2, actionButton3);
87         }
88     }
89
90     /**
91      * Sends a simple notification to log. Log notifications are not pushed to user
92      * devices but are shown to all account users in notifications log.
93      *
94      * @param message the body of the notification
95      *
96      */
97     @ActionDoc(text = "Sends a log notification which is shown in notifications log to all account users")
98     public static void sendLogNotification(String message) {
99         sendLogNotification(message, null, null);
100     }
101
102     /**
103      * Sends an advanced notification to log. Log notifications are not pushed to user
104      * devices but are shown to all account users in notifications log.
105      *
106      * @param message the body of the notification
107      * @param icon name for the notification
108      * @param severity category for the notification
109      *
110      */
111     @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         logger.debug("sending log notification '{}'", message);
114         if (cloudService != null) {
115             cloudService.sendLogNotification(message, icon, severity);
116         }
117     }
118
119     /**
120      * Sends a simple broadcast notification. Broadcast notifications are pushed to all
121      * mobile devices of all users of the account
122      *
123      * @param message the body of the notification
124      *
125      */
126     @ActionDoc(text = "Sends a broadcast notification to all mobile devices of all account users")
127     public static void sendBroadcastNotification(String message) {
128         sendBroadcastNotification(message, null, null);
129     }
130
131     /**
132      * Sends an advanced broadcast notification. Broadcast notifications are pushed to all
133      * mobile devices of all users of the account
134      *
135      * @param message the body of the notification
136      * @param icon name for the notification
137      * @param severity category for the notification
138      *
139      */
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);
143     }
144
145     /**
146      * Sends an advanced broadcast notification. Broadcast notifications are pushed to all
147      * mobile devices of all users of the account
148      *
149      * @param message the body of the notification
150      * @param icon name for the notification
151      * @param severity category for the notification
152      * @param title for the notification
153      * @param onClickAction the action to perform when clicked
154      * @param mediaAttachmentUrl the media to attach to a notification
155      * @param actionButton1 an action button in the format "Title=Action"
156      * @param actionButton2 an action button in the format "Title=Action"
157      * @param actionButton3 an action button in the format "Title=Action"
158      *
159      */
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) {
164         logger.debug("sending broadcast notification '{}' to all users", message);
165         if (cloudService != null) {
166             cloudService.sendBroadcastNotification(message, icon, severity, title, onClickAction, mediaAttachmentUrl,
167                     actionButton1, actionButton2, actionButton3);
168         }
169     }
170 }