1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- using System;
- using System.Collections.Generic;
- using System.Net;
- using System.Net.Http;
- using System.Net.Http.Json;
- using System.Text;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK.Extension;
- namespace TEAMModelOS.SDK.Models.Service
- {
- public class NotificationService
- {
- private readonly HttpClient _httpClient;
- public NotificationService(HttpClient httpClient)
- {
- _httpClient = httpClient;
- }
- public async Task<int> SendNotification(string clientID, string clientSecret, string location, string url, Notification notification) {
- try {
- if (location.Contains("China"))
- {
- location = "China";
- }
- else if (location.Contains("Global"))
- {
- location = "Global";
- }
- var token = await CoreTokenExtensions.CreateAccessToken(clientID, clientSecret, location);
- _httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {token.AccessToken}");
- HttpResponseMessage responseMessage = await _httpClient.PostAsJsonAsync(url, notification);
- if (responseMessage.StatusCode == HttpStatusCode.OK)
- {
- return 200;
- }
- else if (responseMessage.StatusCode == HttpStatusCode.Unauthorized)
- {
- return 401;
- }
- else
- {
- return 500;
- }
- } catch (Exception ) {
- return 500;
- }
- }
- }
- public class Notification {
- /// <summary>
- /// App name (hita) 小寫
- /// </summary>
- public string hubName { get; set; }
- /// <summary>
- /// 通知訊息種類,分為msg,info及notice。
- /// msg : 一般訊息,會存Redis,拿了就刪。
- /// info : 公告訊息或給多人讀取的訊息,會存Redis,拿了不刪,只能等時間到期或透過API刪除。
- /// notice : 屬於系統層級訊息,不存Redis,直接裸送訊息。
- /// </summary>
- public string type { get; set; }
- /// <summary>
- /// 送訊息的來源端 格式為"{服務}:{類別}: ..." 如"ies5:hbrd","ies5:hbrd:hb0901"
- /// </summary>
- public string from { get; set; }
- /// <summary>
- /// 接收對象或手機註冊的tag,ID或服務等...
- /// </summary>
- public List<string> to { get; set; }
- /// <summary>
- /// 標題。
- /// </summary>
- public string label { get; set; }
- /// <summary>
- /// 正文。
- /// </summary>
- public string body { get; set; }
- /// <summary>
- /// 該訊息到期時間(UTC),單位為秒,且必須大於現在時間。
- /// </summary>
- public long expires { get; set; }
- }
- }
|