Skip to content

Commit

Permalink
1.2.4
Browse files Browse the repository at this point in the history
推送接口增加channel_id
增加批量单推接口
增加消息统计vip接口
  • Loading branch information
zheng93775 authored and zhengjh committed Nov 26, 2019
1 parent 4cb59ef commit 4d98f20
Show file tree
Hide file tree
Showing 13 changed files with 446 additions and 87 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,5 @@ NETFrameworkTest/NETFrameworkTest\.csproj
NETFrameworkTest/Program\.cs

NETFrameworkTest/Properties/AssemblyInfo\.cs
/.vscode
/Example/ExampleConfig.cs
68 changes: 62 additions & 6 deletions Example/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ namespace Example
{
class Example
{
private static JPushClient client = new JPushClient("Your AppKey", "Your MasterSecret");
private static JPushClient client = new JPushClient(ExampleConfig.APP_KEY, ExampleConfig.MASTER_SECRET);

public static void Main(string[] args)
{
ExecutePushExample();
ExecuteDeviceEample();
ExecuteBatchPushExample();
ExecuteDeviceExample();
ExecuteReportExample();
ExecuteReceivedDetailReportExample();
ExecuteMessagesDetialReportExample();
ExecuteScheduleExample();

Console.ReadLine();
Expand Down Expand Up @@ -48,16 +51,57 @@ private static void ExecutePushExample()
["key1"] = "value1"
}
},
Options = new Options
{
IsApnsProduction = true // 设置 iOS 推送生产环境。不设置默认为开发环境。
Options = new Options
{
IsApnsProduction = true // 设置 iOS 推送生产环境。不设置默认为开发环境。
}
};
var response = client.SendPush(pushPayload);
Console.WriteLine(response.Content);
}

private static void ExecuteDeviceEample()
private static void ExecuteBatchPushExample()
{
SinglePayload singlePayload = new SinglePayload()
{
Platform = new List<string> { "android", "ios" },
Target = "flink",
Notification = new Notification
{
Alert = "hello jpush",
Android = new Android
{
Alert = "android alert",
Title = "title"
},
IOS = new IOS
{
Alert = "ios alert",
Badge = "+1"
}
},
Message = new Message
{
Title = "message title",
Content = "message content",
Extras = new Dictionary<string, string>
{
["key1"] = "value1"
}
},
Options = new Options
{
IsApnsProduction = true // 设置 iOS 推送生产环境。不设置默认为开发环境。
}
};
List<SinglePayload> singlePayloads = new List<SinglePayload>();
singlePayloads.Add(singlePayload);
Console.WriteLine("start send");
var response = client.BatchPushByAlias(singlePayloads);
Console.WriteLine(response.Content);
}

private static void ExecuteDeviceExample()
{
var registrationId = "12145125123151";
var devicePayload = new DevicePayload
Expand All @@ -80,6 +124,18 @@ private static void ExecuteReportExample()
Console.WriteLine(response.Content);
}

private static void ExecuteReceivedDetailReportExample()
{
var response = client.Report.GetReceivedDetailReport(new List<string> { "1251231231" });
Console.WriteLine(response.Content);
}

private static void ExecuteMessagesDetialReportExample()
{
var response = client.Report.GetMessagesDetailReport(new List<string> { "1251231231" });
Console.WriteLine(response.Content);
}

private static void ExecuteScheduleExample()
{
var pushPayload = new PushPayload
Expand Down
10 changes: 10 additions & 0 deletions Example/ExampleConfig.cs.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@


namespace Example
{
class ExampleConfig
{
public const string APP_KEY = "Your AppKey";
public const string MASTER_SECRET = "Your MasterSecret";
}
}
170 changes: 120 additions & 50 deletions Jiguang.JPush/JPushClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Jiguang.JPush
{
Expand Down Expand Up @@ -42,21 +45,21 @@ public JPushClient(string appKey, string masterSecret)
Report = new ReportClient();
Device = new DeviceClient();
Schedule = new ScheduleClient();
}

/// <summary>
/// 设置 push 功能的 API 调用地址。
/// <para>
/// 如果极光应用分配在北京机房(极光控制台 “应用设置” -> “应用信息” 中可以看到),并且开发者接口调用的服务器也位于北京,则可以调用如下地址:
///
/// https://bjapi.push.jiguang.cn/v3/push
/// <para>可以提升 API 的响应速度。</para>
/// </para>
/// </summary>
}

/// <summary>
/// 设置 push 功能的 API 调用地址。
/// <para>
/// 如果极光应用分配在北京机房(极光控制台 “应用设置” -> “应用信息” 中可以看到),并且开发者接口调用的服务器也位于北京,则可以调用如下地址:
///
/// https://bjapi.push.jiguang.cn/v3/push
/// <para>可以提升 API 的响应速度。</para>
/// </para>
/// </summary>
/// <param name="url"><see cref="BASE_URL_DEFAULT"/> or <see cref="BASE_URL_BEIJING"/></param>
public void SetBaseURL(string url)
{
BASE_URL = url;
public void SetBaseURL(string url)
{
BASE_URL = url;
}

public async Task<HttpResponse> SendPushAsync(string jsonBody)
Expand All @@ -70,9 +73,9 @@ public async Task<HttpResponse> SendPushAsync(string jsonBody)
return new HttpResponse(msg.StatusCode, msg.Headers, content);
}

/// <summary>
/// <see cref="SendPush(PushPayload)"/>
/// </summary>
/// <summary>
/// <see cref="SendPush(PushPayload)"/>
/// </summary>
public async Task<HttpResponse> SendPushAsync(PushPayload payload)
{
if (payload == null)
Expand All @@ -82,16 +85,16 @@ public async Task<HttpResponse> SendPushAsync(PushPayload payload)
return await SendPushAsync(body);
}

/// <summary>
/// 进行消息推送。
/// <see cref="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#_1"/>
/// </summary>
/// <param name="pushPayload"> 推送对象。<see cref="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#_7"/> </param>
public HttpResponse SendPush(PushPayload pushPayload)
{
Task<HttpResponse> task = Task.Run(() => SendPushAsync(pushPayload));
task.Wait();
return task.Result;
/// <summary>
/// 进行消息推送。
/// <see cref="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#_1"/>
/// </summary>
/// <param name="pushPayload"> 推送对象。<see cref="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#_7"/> </param>
public HttpResponse SendPush(PushPayload pushPayload)
{
Task<HttpResponse> task = Task.Run(() => SendPushAsync(pushPayload));
task.Wait();
return task.Result;
}

public async Task<HttpResponse> IsPushValidAsync(string jsonBody)
Expand All @@ -106,9 +109,9 @@ public async Task<HttpResponse> IsPushValidAsync(string jsonBody)
return new HttpResponse(msg.StatusCode, msg.Headers, content);
}

/// <summary>
/// <see cref="IsPushValid(PushPayload)"/>
/// </summary>
/// <summary>
/// <see cref="IsPushValid(PushPayload)"/>
/// </summary>
public async Task<HttpResponse> IsPushValidAsync(PushPayload payload)
{
if (payload == null)
Expand All @@ -118,20 +121,20 @@ public async Task<HttpResponse> IsPushValidAsync(PushPayload payload)
return await IsPushValidAsync(body);
}

/// <summary>
/// 校验推送能否成功。与推送 API 的区别在于:不会实际向用户发送任何消息。 其他字段说明和推送 API 完全相同。
/// </summary>
/// <param name="pushPayload"> 推送对象。<see cref="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#_7"/> </param>
public HttpResponse IsPushValid(PushPayload pushPayload)
{
/// <summary>
/// 校验推送能否成功。与推送 API 的区别在于:不会实际向用户发送任何消息。 其他字段说明和推送 API 完全相同。
/// </summary>
/// <param name="pushPayload"> 推送对象。<see cref="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#_7"/> </param>
public HttpResponse IsPushValid(PushPayload pushPayload)
{
Task<HttpResponse> task = Task.Run(() => IsPushValidAsync(pushPayload));
task.Wait();
return task.Result;
}

/// <summary>
/// <see cref="GetCIdList(int?, string)"/>
/// </summary>
/// <summary>
/// <see cref="GetCIdList(int?, string)"/>
/// </summary>
public async Task<HttpResponse> GetCIdListAsync(int? count, string type)
{
if (count != null && count < 1 && count > 1000)
Expand All @@ -152,17 +155,84 @@ public async Task<HttpResponse> GetCIdListAsync(int? count, string type)
return new HttpResponse(msg.StatusCode, msg.Headers, content);
}

/// <summary>
/// 获取 CId(推送的唯一标识符) 列表。
/// <see cref="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#cid"/>
/// </summary>
/// <param name="count">不传默认为 1。范围为[1, 1000]</param>
/// <param name="type">CId 的类型。取值:"push" (默认) 或 "schedule"</param>
public HttpResponse GetCIdList(int? count, string type)
{
Task<HttpResponse> task = Task.Run(() => GetCIdListAsync(count, type));
task.Wait();
return task.Result;
/// <summary>
/// 获取 CId(推送的唯一标识符) 列表。
/// <see cref="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#cid"/>
/// </summary>
/// <param name="count">不传默认为 1。范围为[1, 1000]</param>
/// <param name="type">CId 的类型。取值:"push" (默认) 或 "schedule"</param>
public HttpResponse GetCIdList(int? count, string type)
{
Task<HttpResponse> task = Task.Run(() => GetCIdListAsync(count, type));
task.Wait();
return task.Result;
}

/// <summary>
/// 针对RegID方式批量单推(VIP专属接口)
/// 如果您在给每个用户的推送内容都不同的情况下,可以使用此接口。
/// <see cref="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#vip"/>
/// </summary>
/// <param name="singlePayLoadList">批量单推的载体列表</param>
public async Task<HttpResponse> BatchPushByRegidAsync(List<SinglePayload> singlePayLoadList)
{
var url = BASE_URL + "/batch/regid/single";
return await BatchPushAsync(url, singlePayLoadList);
}

/// <summary>
/// 针对RegID方式批量单推(VIP专属接口)
/// 如果您在给每个用户的推送内容都不同的情况下,可以使用此接口。
/// <see cref="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#vip"/>
/// </summary>
/// <param name="singlePayLoadList">批量单推的载体列表</param>
public HttpResponse BatchPushByRegid(List<SinglePayload> singlePayLoadList)
{
Task<HttpResponse> task = Task.Run(() => BatchPushByRegidAsync(singlePayLoadList));
task.Wait();
return task.Result;
}

/// <summary>
/// 针对Alias方式批量单推(VIP专属接口)
/// 如果您在给每个用户的推送内容都不同的情况下,可以使用此接口。
/// <see cref="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#vip"/>
/// </summary>
/// <param name="singlePayLoadList">批量单推的载体列表</param>
public async Task<HttpResponse> BatchPushByAliasAsync(List<SinglePayload> singlePayLoadList)
{
var url = BASE_URL + "/batch/alias/single";
return await BatchPushAsync(url, singlePayLoadList);
}

/// <summary>
/// 针对Alias方式批量单推(VIP专属接口)
/// 如果您在给每个用户的推送内容都不同的情况下,可以使用此接口。
/// <see cref="https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#vip"/>
/// </summary>
/// <param name="singlePayLoadList">批量单推的载体列表</param>
public HttpResponse BatchPushByAlias(List<SinglePayload> singlePayLoadList)
{
Task<HttpResponse> task = Task.Run(() => BatchPushByAliasAsync(singlePayLoadList));
task.Wait();
return task.Result;
}

private async Task<HttpResponse> BatchPushAsync(String url, List<SinglePayload> singlePayLoadList)
{
HttpResponse cidResponse = await this.GetCIdListAsync(singlePayLoadList.Count, "push");
JObject jObject = (JObject) JsonConvert.DeserializeObject(cidResponse.Content);
JArray jArray = ((JArray) jObject["cidlist"]);
BatchPushPayload batchPushPayload = new BatchPushPayload();
batchPushPayload.Pushlist = new Dictionary<String, SinglePayload>();
for (int i = 0; i < singlePayLoadList.Count; i++)
{
batchPushPayload.Pushlist.Add((String) jArray[i], singlePayLoadList[i]);
}
HttpContent httpContent = new StringContent(batchPushPayload.ToString(), Encoding.UTF8);
HttpResponseMessage msg = await HttpClient.PostAsync(url, httpContent).ConfigureAwait(false);
var content = await msg.Content.ReadAsStringAsync().ConfigureAwait(false);
return new HttpResponse(msg.StatusCode, msg.Headers, content);
}
}
}
6 changes: 3 additions & 3 deletions Jiguang.JPush/Jiguang.JPush.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
<Copyright>MIT</Copyright>
<PackageLicenseUrl></PackageLicenseUrl>
<PackageProjectUrl>https://github.com/jpush/jpush-api-csharp-client</PackageProjectUrl>
<Version>1.2.3</Version>
<Version>1.2.4</Version>
</PropertyGroup>

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

<ItemGroup>
Expand Down
25 changes: 25 additions & 0 deletions Jiguang.JPush/Model/BatchPushPayload.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Newtonsoft.Json;
using System.Collections.Generic;

namespace Jiguang.JPush.Model
{
public class BatchPushPayload
{
[JsonProperty("pushlist")]
public Dictionary<string, SinglePayload> Pushlist { get; set; }

internal string GetJson()
{
return JsonConvert.SerializeObject(this, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore
});
}

public override string ToString()
{
return GetJson();
}
}
}
Loading

0 comments on commit 4d98f20

Please sign in to comment.