using Jiguang.JPush.Model; using System; using System.Collections.Generic; using System.Net.Http; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace Jiguang.JPush { public class DeviceClient { private const string BASE_URL = "https://device.jpush.cn"; /// /// /// public async Task GetDeviceInfoAsync(string registrationId) { if (string.IsNullOrEmpty(registrationId)) throw new ArgumentNullException(registrationId); var url = BASE_URL + "/v3/devices/" + registrationId; HttpResponseMessage msg = await JPushClient.HttpClient.GetAsync(url).ConfigureAwait(false); var content = await msg.Content.ReadAsStringAsync().ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, content); } /// /// 查询指定设备信息。 /// /// /// /// 客户端初始化 JPush 成功后,JPush 服务端会分配一个 Registration ID,作为此设备的标识(同一个手机不同 APP 的 Registration ID 是不同的)。 /// public HttpResponse GetDeviceInfo(string registrationId) { Task task = Task.Run(() => GetDeviceInfoAsync(registrationId)); task.Wait(); return task.Result; } public async Task UpdateDeviceInfoAsync(string registrationId, string json) { if (string.IsNullOrEmpty(registrationId)) throw new ArgumentNullException(nameof(registrationId)); if (string.IsNullOrEmpty(json)) throw new ArgumentNullException(nameof(json)); var url = BASE_URL + "/v3/devices/" + registrationId; HttpContent requestContent = new StringContent(json, Encoding.UTF8); HttpResponseMessage msg = await JPushClient.HttpClient.PostAsync(url, requestContent).ConfigureAwait(false); string responseContent = await msg.Content.ReadAsStringAsync().ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, responseContent); } /// /// /// public async Task UpdateDeviceInfoAsync(string registrationId, DevicePayload devicePayload) { if (string.IsNullOrEmpty(registrationId)) throw new ArgumentNullException(nameof(registrationId)); if (devicePayload == null) throw new ArgumentNullException(nameof(devicePayload)); var json = devicePayload.ToString(); return await UpdateDeviceInfoAsync(registrationId, json); } /// /// 更新设备信息(目前支持 tag, alias 和 mobile)。 /// /// /// /// 客户端初始化 JPush 成功后,JPush 服务端会分配一个 Registration ID,作为此设备的标识(同一个手机不同 APP 的 Registration ID 是不同的)。 /// /// 设备信息对象 public HttpResponse UpdateDeviceInfo(string registrationId, DevicePayload devicePayload) { Task task = Task.Run(() => UpdateDeviceInfoAsync(registrationId, devicePayload)); task.Wait(); return task.Result; } /// /// /// public async Task GetDevicesByAliasAsync(string alias, string platform) { if (string.IsNullOrEmpty(alias)) throw new ArgumentNullException(nameof(alias)); var url = BASE_URL + "/v3/aliases/" + alias; if (!string.IsNullOrEmpty(platform)) url += "?platform=" + platform; HttpResponseMessage msg = await JPushClient.HttpClient.GetAsync(url).ConfigureAwait(false); string responseConetent = await msg.Content.ReadAsStringAsync().ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, responseConetent); } /// /// 获取指定 alias 下的设备,最多输出 10 个。 /// /// /// 要查询的别名(alias) /// "android" 或 "ios", 为 null 则默认为所有平台。 public HttpResponse GetDeviceByAlias(string alias, string platform) { Task task = Task.Run(() => GetDevicesByAliasAsync(alias, platform)); task.Wait(); return task.Result; } /// /// /// public async Task DeleteAliasAsync(string alias, string platform) { if (string.IsNullOrEmpty(alias)) throw new ArgumentNullException(alias); var url = BASE_URL + "/v3/aliases/" + alias; if (!string.IsNullOrEmpty(platform)) url += "?platform=" + platform; HttpResponseMessage msg = await JPushClient.HttpClient.DeleteAsync(url).ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, ""); } /// /// 删除一个别名,以及该别名与设备的绑定关系。 /// /// /// 待删除的别名(alias) /// "android" 或 "ios",为 null 则默认为所有平台。 public HttpResponse DeleteAlias(string alias, string platform) { Task task = Task.Run(() => DeleteAliasAsync(alias, platform)); task.Wait(); return task.Result; } /// /// /// public async Task GetTagsAsync() { var url = BASE_URL + "/v3/tags/"; HttpResponseMessage msg = await JPushClient.HttpClient.GetAsync(url).ConfigureAwait(false); string responseContent = await msg.Content.ReadAsStringAsync().ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, responseContent); } /// /// 获取当前应用的所有标签列表,每个平台最多返回 100 个。 /// /// public HttpResponse GetTags() { Task task = Task.Run(() => GetTagsAsync()); task.Wait(); return task.Result; } /// /// /// public async Task IsDeviceInTagAsync(string registrationId, string tag) { if (string.IsNullOrEmpty(registrationId)) throw new ArgumentNullException(nameof(registrationId)); if (string.IsNullOrEmpty(tag)) throw new ArgumentNullException(nameof(tag)); var url = BASE_URL + "/v3/tags/" + tag + "/registration_ids/" + registrationId; HttpResponseMessage msg = await JPushClient.HttpClient.GetAsync(url).ConfigureAwait(false); string responseContent = await msg.Content.ReadAsStringAsync().ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, responseContent); } /// /// 查询某个设备是否在某个 tag 下。 /// /// /// 设备的 registration id /// 要查询的 tag public HttpResponse IsDeviceInTag(string registrationId, string tag) { Task task = Task.Run(() => IsDeviceInTagAsync(registrationId, tag)); task.Wait(); return task.Result; } /// /// /// public async Task AddDevicesToTagAsync(string tag, List registrationIdList) { if (string.IsNullOrEmpty(tag)) throw new ArgumentNullException(nameof(tag)); if (registrationIdList == null || registrationIdList.Count == 0) throw new ArgumentException(nameof(registrationIdList)); var url = BASE_URL + "/v3/tags/" + tag; JObject jObj = new JObject { ["registration_ids"] = new JObject { ["add"] = new JArray(registrationIdList) } }; var requestContent = new StringContent(jObj.ToString(), Encoding.UTF8); HttpResponseMessage msg = await JPushClient.HttpClient.PostAsync(url, requestContent).ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, ""); } /// /// 为一个标签(tag)添加设备,一次最多支持 1000 个。 /// /// /// 待操作的标签(tag) /// 设备的 registration id 列表 public HttpResponse AddDevicesToTag(string tag, List registrationIdList) { Task task = Task.Run(() => AddDevicesToTagAsync(tag, registrationIdList)); task.Wait(); return task.Result; } /// /// /// public async Task RemoveDevicesFromTagAsync(string tag, List registrationIdList) { if (string.IsNullOrEmpty(tag)) throw new ArgumentNullException(nameof(tag)); if (registrationIdList == null || registrationIdList.Count == 0) throw new ArgumentException(nameof(registrationIdList)); var url = BASE_URL + "/v3/tags/" + tag; JObject jObj = new JObject { ["registration_ids"] = new JObject { ["remove"] = new JArray(registrationIdList) } }; var requestContent = new StringContent(jObj.ToString(), Encoding.UTF8); HttpResponseMessage msg = await JPushClient.HttpClient.PostAsync(url, requestContent).ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, ""); } /// /// 为一个标签移除设备。 /// /// /// 待操作的标签(tag) /// 设备的 registration id 列表 public HttpResponse RemoveDevicesFromTag(string tag, List registrationIdList) { Task task = Task.Run(() => RemoveDevicesFromTagAsync(tag, registrationIdList)); task.Wait(); return task.Result; } /// /// /// public async Task DeleteTagAsync(string tag, string platform) { if (string.IsNullOrEmpty(tag)) throw new ArgumentNullException(nameof(tag)); var url = BASE_URL + "/v3/tags/" + tag; if (!string.IsNullOrEmpty(platform)) url += "?platform=" + platform; HttpResponseMessage msg = await JPushClient.HttpClient.DeleteAsync(url).ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, ""); } /// /// 删除标签,以及标签与其下设备的关联关系。 /// /// /// 待删除标签 /// "android" 或 "ios",如果为 null,则默认为所有平台 public HttpResponse DeleteTag(string tag, string platform) { Task task = Task.Run(() => DeleteTagAsync(tag, platform)); task.Wait(); return task.Result; } /// /// /// public async Task GetUserOnlineStatusAsync(List registrationIdList) { if (registrationIdList == null || registrationIdList.Count == 0) throw new ArgumentException(nameof(registrationIdList)); var url = BASE_URL + "/v3/devices/status/"; JObject jObj = new JObject { ["registration_ids"] = new JArray(registrationIdList) }; var requestContent = new StringContent(jObj.ToString(), Encoding.UTF8); HttpResponseMessage msg = await JPushClient.HttpClient.PostAsync(url, requestContent).ConfigureAwait(false); string responseContent = await msg.Content.ReadAsStringAsync().ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, responseContent); } /// /// 获取用户在线状态(VIP only)。 /// /// /// 待查询用户设备的 registration id,每次最多支持 1000 个。 public HttpResponse GetUserOnlineStatus(List registrationIdList) { Task task = Task.Run(() => GetUserOnlineStatusAsync(registrationIdList)); task.Wait(); return task.Result; } } }