44using System . Net . Http . Headers ;
55using System . Text ;
66using System . Threading . Tasks ;
7+ using System . Collections . Generic ;
8+ using Newtonsoft . Json ;
9+ using Newtonsoft . Json . Linq ;
710
811namespace Jiguang . JPush
912{
@@ -42,21 +45,21 @@ public JPushClient(string appKey, string masterSecret)
4245 Report = new ReportClient ( ) ;
4346 Device = new DeviceClient ( ) ;
4447 Schedule = new ScheduleClient ( ) ;
45- }
46-
47- /// <summary>
48- /// 设置 push 功能的 API 调用地址。
49- /// <para>
50- /// 如果极光应用分配在北京机房(极光控制台 “应用设置” -> “应用信息” 中可以看到),并且开发者接口调用的服务器也位于北京,则可以调用如下地址:
51- ///
52- /// https://bjapi.push.jiguang.cn/v3/push
53- /// <para>可以提升 API 的响应速度。</para>
54- /// </para>
55- /// </summary>
48+ }
49+
50+ /// <summary>
51+ /// 设置 push 功能的 API 调用地址。
52+ /// <para>
53+ /// 如果极光应用分配在北京机房(极光控制台 “应用设置” -> “应用信息” 中可以看到),并且开发者接口调用的服务器也位于北京,则可以调用如下地址:
54+ ///
55+ /// https://bjapi.push.jiguang.cn/v3/push
56+ /// <para>可以提升 API 的响应速度。</para>
57+ /// </para>
58+ /// </summary>
5659 /// <param name="url"><see cref="BASE_URL_DEFAULT"/> or <see cref="BASE_URL_BEIJING"/></param>
57- public void SetBaseURL ( string url )
58- {
59- BASE_URL = url ;
60+ public void SetBaseURL ( string url )
61+ {
62+ BASE_URL = url ;
6063 }
6164
6265 public async Task < HttpResponse > SendPushAsync ( string jsonBody )
@@ -70,9 +73,9 @@ public async Task<HttpResponse> SendPushAsync(string jsonBody)
7073 return new HttpResponse ( msg . StatusCode , msg . Headers , content ) ;
7174 }
7275
73- /// <summary>
74- /// <see cref="SendPush(PushPayload)"/>
75- /// </summary>
76+ /// <summary>
77+ /// <see cref="SendPush(PushPayload)"/>
78+ /// </summary>
7679 public async Task < HttpResponse > SendPushAsync ( PushPayload payload )
7780 {
7881 if ( payload == null )
@@ -82,16 +85,16 @@ public async Task<HttpResponse> SendPushAsync(PushPayload payload)
8285 return await SendPushAsync ( body ) ;
8386 }
8487
85- /// <summary>
86- /// 进行消息推送。
87- /// <see cref="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#_1"/>
88- /// </summary>
89- /// <param name="pushPayload"> 推送对象。<see cref="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#_7"/> </param>
90- public HttpResponse SendPush ( PushPayload pushPayload )
91- {
92- Task < HttpResponse > task = Task . Run ( ( ) => SendPushAsync ( pushPayload ) ) ;
93- task . Wait ( ) ;
94- return task . Result ;
88+ /// <summary>
89+ /// 进行消息推送。
90+ /// <see cref="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#_1"/>
91+ /// </summary>
92+ /// <param name="pushPayload"> 推送对象。<see cref="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#_7"/> </param>
93+ public HttpResponse SendPush ( PushPayload pushPayload )
94+ {
95+ Task < HttpResponse > task = Task . Run ( ( ) => SendPushAsync ( pushPayload ) ) ;
96+ task . Wait ( ) ;
97+ return task . Result ;
9598 }
9699
97100 public async Task < HttpResponse > IsPushValidAsync ( string jsonBody )
@@ -106,9 +109,9 @@ public async Task<HttpResponse> IsPushValidAsync(string jsonBody)
106109 return new HttpResponse ( msg . StatusCode , msg . Headers , content ) ;
107110 }
108111
109- /// <summary>
110- /// <see cref="IsPushValid(PushPayload)"/>
111- /// </summary>
112+ /// <summary>
113+ /// <see cref="IsPushValid(PushPayload)"/>
114+ /// </summary>
112115 public async Task < HttpResponse > IsPushValidAsync ( PushPayload payload )
113116 {
114117 if ( payload == null )
@@ -118,20 +121,20 @@ public async Task<HttpResponse> IsPushValidAsync(PushPayload payload)
118121 return await IsPushValidAsync ( body ) ;
119122 }
120123
121- /// <summary>
122- /// 校验推送能否成功。与推送 API 的区别在于:不会实际向用户发送任何消息。 其他字段说明和推送 API 完全相同。
123- /// </summary>
124- /// <param name="pushPayload"> 推送对象。<see cref="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#_7"/> </param>
125- public HttpResponse IsPushValid ( PushPayload pushPayload )
126- {
124+ /// <summary>
125+ /// 校验推送能否成功。与推送 API 的区别在于:不会实际向用户发送任何消息。 其他字段说明和推送 API 完全相同。
126+ /// </summary>
127+ /// <param name="pushPayload"> 推送对象。<see cref="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#_7"/> </param>
128+ public HttpResponse IsPushValid ( PushPayload pushPayload )
129+ {
127130 Task < HttpResponse > task = Task . Run ( ( ) => IsPushValidAsync ( pushPayload ) ) ;
128131 task . Wait ( ) ;
129132 return task . Result ;
130133 }
131134
132- /// <summary>
133- /// <see cref="GetCIdList(int?, string)"/>
134- /// </summary>
135+ /// <summary>
136+ /// <see cref="GetCIdList(int?, string)"/>
137+ /// </summary>
135138 public async Task < HttpResponse > GetCIdListAsync ( int ? count , string type )
136139 {
137140 if ( count != null && count < 1 && count > 1000 )
@@ -152,17 +155,84 @@ public async Task<HttpResponse> GetCIdListAsync(int? count, string type)
152155 return new HttpResponse ( msg . StatusCode , msg . Headers , content ) ;
153156 }
154157
155- /// <summary>
156- /// 获取 CId(推送的唯一标识符) 列表。
157- /// <see cref="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#cid"/>
158- /// </summary>
159- /// <param name="count">不传默认为 1。范围为[1, 1000]</param>
160- /// <param name="type">CId 的类型。取值:"push" (默认) 或 "schedule"</param>
161- public HttpResponse GetCIdList ( int ? count , string type )
162- {
163- Task < HttpResponse > task = Task . Run ( ( ) => GetCIdListAsync ( count , type ) ) ;
164- task . Wait ( ) ;
165- return task . Result ;
158+ /// <summary>
159+ /// 获取 CId(推送的唯一标识符) 列表。
160+ /// <see cref="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#cid"/>
161+ /// </summary>
162+ /// <param name="count">不传默认为 1。范围为[1, 1000]</param>
163+ /// <param name="type">CId 的类型。取值:"push" (默认) 或 "schedule"</param>
164+ public HttpResponse GetCIdList ( int ? count , string type )
165+ {
166+ Task < HttpResponse > task = Task . Run ( ( ) => GetCIdListAsync ( count , type ) ) ;
167+ task . Wait ( ) ;
168+ return task . Result ;
169+ }
170+
171+ /// <summary>
172+ /// 针对RegID方式批量单推(VIP专属接口)
173+ /// 如果您在给每个用户的推送内容都不同的情况下,可以使用此接口。
174+ /// <see cref="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#vip"/>
175+ /// </summary>
176+ /// <param name="singlePayLoadList">批量单推的载体列表</param>
177+ public async Task < HttpResponse > BatchPushByRegidAsync ( List < SinglePayload > singlePayLoadList )
178+ {
179+ var url = BASE_URL + "/batch/regid/single" ;
180+ return await BatchPushAsync ( url , singlePayLoadList ) ;
181+ }
182+
183+ /// <summary>
184+ /// 针对RegID方式批量单推(VIP专属接口)
185+ /// 如果您在给每个用户的推送内容都不同的情况下,可以使用此接口。
186+ /// <see cref="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#vip"/>
187+ /// </summary>
188+ /// <param name="singlePayLoadList">批量单推的载体列表</param>
189+ public HttpResponse BatchPushByRegid ( List < SinglePayload > singlePayLoadList )
190+ {
191+ Task < HttpResponse > task = Task . Run ( ( ) => BatchPushByRegidAsync ( singlePayLoadList ) ) ;
192+ task . Wait ( ) ;
193+ return task . Result ;
194+ }
195+
196+ /// <summary>
197+ /// 针对Alias方式批量单推(VIP专属接口)
198+ /// 如果您在给每个用户的推送内容都不同的情况下,可以使用此接口。
199+ /// <see cref="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#vip"/>
200+ /// </summary>
201+ /// <param name="singlePayLoadList">批量单推的载体列表</param>
202+ public async Task < HttpResponse > BatchPushByAliasAsync ( List < SinglePayload > singlePayLoadList )
203+ {
204+ var url = BASE_URL + "/batch/alias/single" ;
205+ return await BatchPushAsync ( url , singlePayLoadList ) ;
206+ }
207+
208+ /// <summary>
209+ /// 针对Alias方式批量单推(VIP专属接口)
210+ /// 如果您在给每个用户的推送内容都不同的情况下,可以使用此接口。
211+ /// <see cref="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#vip"/>
212+ /// </summary>
213+ /// <param name="singlePayLoadList">批量单推的载体列表</param>
214+ public HttpResponse BatchPushByAlias ( List < SinglePayload > singlePayLoadList )
215+ {
216+ Task < HttpResponse > task = Task . Run ( ( ) => BatchPushByAliasAsync ( singlePayLoadList ) ) ;
217+ task . Wait ( ) ;
218+ return task . Result ;
219+ }
220+
221+ private async Task < HttpResponse > BatchPushAsync ( String url , List < SinglePayload > singlePayLoadList )
222+ {
223+ HttpResponse cidResponse = await this . GetCIdListAsync ( singlePayLoadList . Count , "push" ) ;
224+ JObject jObject = ( JObject ) JsonConvert . DeserializeObject ( cidResponse . Content ) ;
225+ JArray jArray = ( ( JArray ) jObject [ "cidlist" ] ) ;
226+ BatchPushPayload batchPushPayload = new BatchPushPayload ( ) ;
227+ batchPushPayload . Pushlist = new Dictionary < String , SinglePayload > ( ) ;
228+ for ( int i = 0 ; i < singlePayLoadList . Count ; i ++ )
229+ {
230+ batchPushPayload . Pushlist . Add ( ( String ) jArray [ i ] , singlePayLoadList [ i ] ) ;
231+ }
232+ HttpContent httpContent = new StringContent ( batchPushPayload . ToString ( ) , Encoding . UTF8 ) ;
233+ HttpResponseMessage msg = await HttpClient . PostAsync ( url , httpContent ) . ConfigureAwait ( false ) ;
234+ var content = await msg . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
235+ return new HttpResponse ( msg . StatusCode , msg . Headers , content ) ;
166236 }
167237 }
168238}
0 commit comments