40
40
import org .eclipse .jetty .http .HttpStatus ;
41
41
import org .eclipse .jetty .util .BufferUtil ;
42
42
import org .eclipse .jetty .util .URIUtil ;
43
+ import org .json .JSONArray ;
43
44
import org .json .JSONException ;
44
45
import org .json .JSONObject ;
45
46
import org .openhab .core .OpenHAB ;
70
71
*
71
72
* @author Victor Belov - Initial contribution
72
73
* @author Kai Kreuzer - migrated code to new Jetty client and ESH APIs
74
+ * @author Dan Cunningham - Extended notification enhancements
73
75
*/
74
76
public class CloudClient {
75
77
@@ -587,39 +589,67 @@ private void handleCommandEvent(JSONObject data) {
587
589
* @param message notification message text
588
590
* @param icon name of the icon for this notification
589
591
* @param severity severity name for this notification
592
+ * @param title for the notification
593
+ * @param onClickAction the action to perform when clicked
594
+ * @param mediaAttachmentUrl the media to attach to a notification
595
+ * @param actionButton1 an action button in the format "Title=Action"
596
+ * @param actionButton2 an action button in the format "Title=Action"
597
+ * @param actionButton3 an action button in the format "Title=Action"
590
598
*/
591
- public void sendNotification (String userId , String message , @ Nullable String icon , @ Nullable String severity ) {
592
- if (isConnected ()) {
593
- JSONObject notificationMessage = new JSONObject ();
594
- try {
595
- notificationMessage .put ("userId" , userId );
596
- notificationMessage .put ("message" , message );
597
- notificationMessage .put ("icon" , icon );
598
- notificationMessage .put ("severity" , severity );
599
- socket .emit ("notification" , notificationMessage );
600
- } catch (JSONException e ) {
601
- logger .debug ("{}" , e .getMessage ());
602
- }
603
- } else {
604
- logger .debug ("No connection, notification is not sent" );
605
- }
599
+ public void sendNotification (String userId , String message , @ Nullable String icon , @ Nullable String severity ,
600
+ @ Nullable String title , @ Nullable String onClickAction , @ Nullable String mediaAttachmentUrl ,
601
+ @ Nullable String actionButton1 , @ Nullable String actionButton2 , @ Nullable String actionButton3 ) {
602
+ sendNotificationInternal (userId , message , icon , severity , title , onClickAction , mediaAttachmentUrl ,
603
+ actionButton1 , actionButton2 , actionButton3 );
606
604
}
607
605
608
606
/**
609
- * This method sends log notification to the openHAB Cloud
607
+ * This method sends broadcast notification to the openHAB Cloud
610
608
*
611
609
* @param message notification message text
612
610
* @param icon name of the icon for this notification
613
611
* @param severity severity name for this notification
612
+ * @param title for this notification
613
+ * @param onClickAction the action to perform when clicked
614
+ * @param mediaAttachmentUrl the media to attach to a notification
615
+ * @param actionButton1 an action button in the format "Title=Action"
616
+ * @param actionButton2 an action button in the format "Title=Action"
617
+ * @param actionButton3 an action button in the format "Title=Action"
614
618
*/
615
- public void sendLogNotification (String message , @ Nullable String icon , @ Nullable String severity ) {
619
+ public void sendBroadcastNotification (String message , @ Nullable String icon , @ Nullable String severity ,
620
+ @ Nullable String title , @ Nullable String onClickAction , @ Nullable String mediaAttachmentUrl ,
621
+ @ Nullable String actionButton1 , @ Nullable String actionButton2 , @ Nullable String actionButton3 ) {
622
+ sendNotificationInternal (null , message , icon , severity , title , onClickAction , mediaAttachmentUrl , actionButton1 ,
623
+ actionButton2 , actionButton3 );
624
+ }
625
+
626
+ private void sendNotificationInternal (@ Nullable String userId , String message , @ Nullable String icon ,
627
+ @ Nullable String severity , @ Nullable String title , @ Nullable String onClickAction ,
628
+ @ Nullable String mediaAttachmentUrl , @ Nullable String actionButton1 , @ Nullable String actionButton2 ,
629
+ @ Nullable String actionButton3 ) {
616
630
if (isConnected ()) {
617
631
JSONObject notificationMessage = new JSONObject ();
618
632
try {
633
+ if (userId != null ) {
634
+ notificationMessage .put ("userId" , userId );
635
+ }
619
636
notificationMessage .put ("message" , message );
620
637
notificationMessage .put ("icon" , icon );
621
638
notificationMessage .put ("severity" , severity );
622
- socket .emit ("lognotification" , notificationMessage );
639
+ if (title != null ) {
640
+ notificationMessage .put ("title" , title );
641
+ }
642
+ if (onClickAction != null ) {
643
+ notificationMessage .put ("on-click" , onClickAction );
644
+ }
645
+ if (mediaAttachmentUrl != null ) {
646
+ notificationMessage .put ("media-attachment-url" , mediaAttachmentUrl );
647
+ }
648
+ JSONArray actionArray = createActionArray (actionButton1 , actionButton2 , actionButton3 );
649
+ if (!actionArray .isEmpty ()) {
650
+ notificationMessage .put ("actions" , actionArray );
651
+ }
652
+ socket .emit (userId == null ? "broadcastnotification" : "notification" , notificationMessage );
623
653
} catch (JSONException e ) {
624
654
logger .debug ("{}" , e .getMessage ());
625
655
}
@@ -629,20 +659,20 @@ public void sendLogNotification(String message, @Nullable String icon, @Nullable
629
659
}
630
660
631
661
/**
632
- * This method sends broadcast notification to the openHAB Cloud
662
+ * This method sends log notification to the openHAB Cloud
633
663
*
634
664
* @param message notification message text
635
665
* @param icon name of the icon for this notification
636
666
* @param severity severity name for this notification
637
667
*/
638
- public void sendBroadcastNotification (String message , @ Nullable String icon , @ Nullable String severity ) {
668
+ public void sendLogNotification (String message , @ Nullable String icon , @ Nullable String severity ) {
639
669
if (isConnected ()) {
640
670
JSONObject notificationMessage = new JSONObject ();
641
671
try {
642
672
notificationMessage .put ("message" , message );
643
673
notificationMessage .put ("icon" , icon );
644
674
notificationMessage .put ("severity" , severity );
645
- socket .emit ("broadcastnotification " , notificationMessage );
675
+ socket .emit ("lognotification " , notificationMessage );
646
676
} catch (JSONException e ) {
647
677
logger .debug ("{}" , e .getMessage ());
648
678
}
@@ -715,6 +745,23 @@ private JSONObject getJSONHeaders(HttpFields httpFields) {
715
745
return headersJSON ;
716
746
}
717
747
748
+ private JSONArray createActionArray (@ Nullable String ... actionStrings ) {
749
+ JSONArray actionArray = new JSONArray ();
750
+ for (String actionString : actionStrings ) {
751
+ if (actionString == null ) {
752
+ continue ;
753
+ }
754
+ String [] parts = actionString .split ("=" , 2 );
755
+ if (parts .length == 2 ) {
756
+ JSONObject action = new JSONObject ();
757
+ action .put ("title" , parts [0 ]);
758
+ action .put ("action" , parts [1 ]);
759
+ actionArray .put (action );
760
+ }
761
+ }
762
+ return actionArray ;
763
+ }
764
+
718
765
private static String censored (String secret ) {
719
766
if (secret .length () < 4 ) {
720
767
return "*******" ;
0 commit comments