11package cn .jpush .api .push .model .live_activity ;
22
33import cn .jpush .api .push .model .PushModel ;
4- import com .google .gson .JsonArray ;
5- import com .google .gson .JsonElement ;
6- import com .google .gson .JsonObject ;
7- import com .google .gson .JsonPrimitive ;
4+ import com .google .gson .*;
85
96public class LiveActivity implements PushModel {
107
@@ -15,13 +12,15 @@ public class LiveActivity implements PushModel {
1512 private final String iOSEvent ;
1613 private final JsonObject iOSContentState ;
1714 private final Long iOSDismissalDate ;
15+ private final JsonObject iOSAlert ;
1816
19- public LiveActivity (Boolean apnsProduction , String liveActivityId , String iOSEvent , JsonObject iOSContentState , Long iOSDismissalDate ) {
17+ public LiveActivity (Boolean apnsProduction , String liveActivityId , String iOSEvent , JsonObject iOSContentState , Long iOSDismissalDate , JsonObject iOSAlert ) {
2018 this .apnsProduction = apnsProduction ;
2119 this .liveActivityId = liveActivityId ;
2220 this .iOSEvent = iOSEvent ;
2321 this .iOSContentState = iOSContentState ;
2422 this .iOSDismissalDate = iOSDismissalDate ;
23+ this .iOSAlert = iOSAlert ;
2524 }
2625
2726 public static Builder newBuilder () {
@@ -34,6 +33,7 @@ public static class Builder {
3433 private String iOSEvent ;
3534 private JsonObject iOSContentState ;
3635 private Long iOSDismissalDate ;
36+ private JsonObject iOSAlert ;
3737
3838 public Builder apnsProduction (Boolean apnsProduction ) {
3939 this .apnsProduction = apnsProduction ;
@@ -81,15 +81,55 @@ public Builder iOSDismissalDate(Long iOSDismissalDate) {
8181 return this ;
8282 }
8383
84+ public Builder iOSAlertTitle (String iosAlertTitle ) {
85+ if (this .iOSAlert == null ) {
86+ this .iOSAlert = new JsonObject ();
87+ }
88+ this .iOSAlert .addProperty ("title" , iosAlertTitle );
89+ return this ;
90+ }
91+
92+ public Builder iOSAlertAlternateTitle (String iosAlertAlternateTitle ) {
93+ if (this .iOSAlert == null ) {
94+ this .iOSAlert = new JsonObject ();
95+ }
96+ this .iOSAlert .addProperty ("alternate_title" , iosAlertAlternateTitle );
97+ return this ;
98+ }
99+
100+ public Builder iOSAlertBody (String iosAlertBody ) {
101+ if (this .iOSAlert == null ) {
102+ this .iOSAlert = new JsonObject ();
103+ }
104+ this .iOSAlert .addProperty ("body" , iosAlertBody );
105+ return this ;
106+ }
107+
108+ public Builder iOSAlertAlternateBody (String iosAlertAlternateBody ) {
109+ if (this .iOSAlert == null ) {
110+ this .iOSAlert = new JsonObject ();
111+ }
112+ this .iOSAlert .addProperty ("alternate_body" , iosAlertAlternateBody );
113+ return this ;
114+ }
115+
116+ public Builder iOSAlertSound (String iosAlertSound ) {
117+ if (this .iOSAlert == null ) {
118+ this .iOSAlert = new JsonObject ();
119+ }
120+ this .iOSAlert .addProperty ("sound" , iosAlertSound );
121+ return this ;
122+ }
123+
84124 public LiveActivity build () {
85- return new LiveActivity (apnsProduction , liveActivityId , iOSEvent , iOSContentState , iOSDismissalDate );
125+ return new LiveActivity (apnsProduction , liveActivityId , iOSEvent , iOSContentState , iOSDismissalDate , iOSAlert );
86126 }
87127
88128 }
89129
90130 @ Override
91131 public JsonElement toJSON () {
92- JsonObject jsonObject = new JsonObject ();
132+ JsonObject pushJsonObject = new JsonObject ();
93133
94134 JsonArray platformJsonArray = new JsonArray ();
95135 platformJsonArray .add (new JsonPrimitive ("ios" ));
@@ -99,37 +139,42 @@ public JsonElement toJSON() {
99139 audienceJsonObject .addProperty ("live_activity_id" , liveActivityId );
100140 }
101141
102- JsonObject optionsJsonObject = new JsonObject ();
103- if (apnsProduction != null ) {
104- optionsJsonObject .addProperty ("apns_production" , apnsProduction );
105- }
106-
107142 JsonObject liveActivityJsonObject = new JsonObject ();
108143 JsonObject iOSJsonObject = new JsonObject ();
109- JsonObject alertJsonObject = new JsonObject ();
110144
111145 if (iOSEvent != null ) {
112146 iOSJsonObject .addProperty ("event" , iOSEvent );
113147 }
148+
114149 if (iOSContentState != null ) {
115150 iOSJsonObject .add ("content-state" , iOSContentState );
116151 }
117- if (!alertJsonObject .entrySet ().isEmpty ()) {
118- iOSJsonObject .add ("alert" , alertJsonObject );
119- }
120- if (!alertJsonObject .entrySet ().isEmpty ()) {
152+
153+ if (iOSDismissalDate != null ) {
121154 iOSJsonObject .addProperty ("dismissal-date" , iOSDismissalDate );
122155 }
123156
157+ if (iOSAlert != null ) {
158+ iOSJsonObject .add ("alert" , iOSAlert );
159+ }
160+
124161 if (!iOSJsonObject .entrySet ().isEmpty ()) {
125162 liveActivityJsonObject .add ("ios" , iOSJsonObject );
126163 }
127164
128- jsonObject .add ("platform" , platformJsonArray );
129- jsonObject .add ("audience" , audienceJsonObject );
130- jsonObject .add ("live_activity" , liveActivityJsonObject );
131- jsonObject .add ("options" , optionsJsonObject );
132- return jsonObject ;
165+ JsonObject optionsJsonObject = new JsonObject ();
166+ if (apnsProduction != null ) {
167+ optionsJsonObject .addProperty ("apns_production" , apnsProduction );
168+ }
169+ if (iOSAlert != null ) {
170+ optionsJsonObject .addProperty ("alternate_set" , true );
171+ }
172+
173+ pushJsonObject .add ("platform" , platformJsonArray );
174+ pushJsonObject .add ("audience" , audienceJsonObject );
175+ pushJsonObject .add ("live_activity" , liveActivityJsonObject );
176+ pushJsonObject .add ("options" , optionsJsonObject );
177+ return pushJsonObject ;
133178 }
134179
135180}
0 commit comments