Skip to content

Commit 4d98f20

Browse files
zheng93775zhengjh
authored andcommitted
1.2.4
推送接口增加channel_id 增加批量单推接口 增加消息统计vip接口
1 parent 4cb59ef commit 4d98f20

File tree

13 files changed

+446
-87
lines changed

13 files changed

+446
-87
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,3 +434,5 @@ NETFrameworkTest/NETFrameworkTest\.csproj
434434
NETFrameworkTest/Program\.cs
435435

436436
NETFrameworkTest/Properties/AssemblyInfo\.cs
437+
/.vscode
438+
/Example/ExampleConfig.cs

Example/Example.cs

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ namespace Example
77
{
88
class Example
99
{
10-
private static JPushClient client = new JPushClient("Your AppKey", "Your MasterSecret");
10+
private static JPushClient client = new JPushClient(ExampleConfig.APP_KEY, ExampleConfig.MASTER_SECRET);
1111

1212
public static void Main(string[] args)
1313
{
1414
ExecutePushExample();
15-
ExecuteDeviceEample();
15+
ExecuteBatchPushExample();
16+
ExecuteDeviceExample();
1617
ExecuteReportExample();
18+
ExecuteReceivedDetailReportExample();
19+
ExecuteMessagesDetialReportExample();
1720
ExecuteScheduleExample();
1821

1922
Console.ReadLine();
@@ -48,16 +51,57 @@ private static void ExecutePushExample()
4851
["key1"] = "value1"
4952
}
5053
},
51-
Options = new Options
52-
{
53-
IsApnsProduction = true // 设置 iOS 推送生产环境。不设置默认为开发环境。
54+
Options = new Options
55+
{
56+
IsApnsProduction = true // 设置 iOS 推送生产环境。不设置默认为开发环境。
5457
}
5558
};
5659
var response = client.SendPush(pushPayload);
5760
Console.WriteLine(response.Content);
5861
}
5962

60-
private static void ExecuteDeviceEample()
63+
private static void ExecuteBatchPushExample()
64+
{
65+
SinglePayload singlePayload = new SinglePayload()
66+
{
67+
Platform = new List<string> { "android", "ios" },
68+
Target = "flink",
69+
Notification = new Notification
70+
{
71+
Alert = "hello jpush",
72+
Android = new Android
73+
{
74+
Alert = "android alert",
75+
Title = "title"
76+
},
77+
IOS = new IOS
78+
{
79+
Alert = "ios alert",
80+
Badge = "+1"
81+
}
82+
},
83+
Message = new Message
84+
{
85+
Title = "message title",
86+
Content = "message content",
87+
Extras = new Dictionary<string, string>
88+
{
89+
["key1"] = "value1"
90+
}
91+
},
92+
Options = new Options
93+
{
94+
IsApnsProduction = true // 设置 iOS 推送生产环境。不设置默认为开发环境。
95+
}
96+
};
97+
List<SinglePayload> singlePayloads = new List<SinglePayload>();
98+
singlePayloads.Add(singlePayload);
99+
Console.WriteLine("start send");
100+
var response = client.BatchPushByAlias(singlePayloads);
101+
Console.WriteLine(response.Content);
102+
}
103+
104+
private static void ExecuteDeviceExample()
61105
{
62106
var registrationId = "12145125123151";
63107
var devicePayload = new DevicePayload
@@ -80,6 +124,18 @@ private static void ExecuteReportExample()
80124
Console.WriteLine(response.Content);
81125
}
82126

127+
private static void ExecuteReceivedDetailReportExample()
128+
{
129+
var response = client.Report.GetReceivedDetailReport(new List<string> { "1251231231" });
130+
Console.WriteLine(response.Content);
131+
}
132+
133+
private static void ExecuteMessagesDetialReportExample()
134+
{
135+
var response = client.Report.GetMessagesDetailReport(new List<string> { "1251231231" });
136+
Console.WriteLine(response.Content);
137+
}
138+
83139
private static void ExecuteScheduleExample()
84140
{
85141
var pushPayload = new PushPayload

Example/ExampleConfig.cs.example

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
3+
namespace Example
4+
{
5+
class ExampleConfig
6+
{
7+
public const string APP_KEY = "Your AppKey";
8+
public const string MASTER_SECRET = "Your MasterSecret";
9+
}
10+
}

Jiguang.JPush/JPushClient.cs

Lines changed: 120 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
using System.Net.Http.Headers;
55
using System.Text;
66
using System.Threading.Tasks;
7+
using System.Collections.Generic;
8+
using Newtonsoft.Json;
9+
using Newtonsoft.Json.Linq;
710

811
namespace 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
}

Jiguang.JPush/Jiguang.JPush.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
<Copyright>MIT</Copyright>
1313
<PackageLicenseUrl></PackageLicenseUrl>
1414
<PackageProjectUrl>https://github.com/jpush/jpush-api-csharp-client</PackageProjectUrl>
15-
<Version>1.2.3</Version>
15+
<Version>1.2.4</Version>
1616
</PropertyGroup>
1717

18-
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netstandard1.1|AnyCPU'">
19-
<DocumentationFile>bin\Debug\netstandard1.1\Jiguang.JPush.xml</DocumentationFile>
18+
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netstandard1.1|AnyCPU'">
19+
<DocumentationFile>bin\Debug\netstandard1.1\Jiguang.JPush.xml</DocumentationFile>
2020
</PropertyGroup>
2121

2222
<ItemGroup>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Newtonsoft.Json;
2+
using System.Collections.Generic;
3+
4+
namespace Jiguang.JPush.Model
5+
{
6+
public class BatchPushPayload
7+
{
8+
[JsonProperty("pushlist")]
9+
public Dictionary<string, SinglePayload> Pushlist { get; set; }
10+
11+
internal string GetJson()
12+
{
13+
return JsonConvert.SerializeObject(this, new JsonSerializerSettings
14+
{
15+
NullValueHandling = NullValueHandling.Ignore,
16+
DefaultValueHandling = DefaultValueHandling.Ignore
17+
});
18+
}
19+
20+
public override string ToString()
21+
{
22+
return GetJson();
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)