C# Jpush 极光推送消息推送
簡介
消息推送(Push)指運營人員通過自己的產品或第三方工具對用戶移動設備進行的主動消息推送。用戶可以在移動設備鎖定屏幕和通知欄看到push消息通知,通知欄點擊可喚起APP并去往相應頁面。我們平時在鎖屏上看到的微信消息等等都屬于APP消息推送行列。使用極光推送, C# 服務端推送到 Demo App,Android 手機收到推送,整理為十個步驟,使用十分鐘左右,完成從注冊賬號到 Android 手機上的 Demo App 收到推送。
步驟
1.注冊極光賬號 注冊頁面:https://www.jiguang.cn/accounts/register/form
2.創建應用 控制臺:https://www.jiguang.cn/dev/#/app/list
創建應用:https://www.jiguang.cn/dev/#/app/create
創建之后回到應用管理:https://www.jiguang.cn/dev/#/app/list
3.獲取 AppKey 和 Master Secret 點擊應用管理界面的應用詳情 獲取 AppKey 和 Master Secret
4.下載 Demo 在上一步驟的同一界面下載 Demo 點擊掃描下載安裝包
手機下載安裝即可,安裝好之后打開 Demo App。
5.下載 jpush-api-csharp-client 項目地址:https://github.com/jpush/jpush-api-csharp-client/releases
VS 打開項目,安裝依賴 NuGet 包管理工具會下載 jpush-api-csharp-client 和 Newtonsoft 依賴。
7.替換 AppKey 和 Master Secret
主要方法
添加標簽
public?Result?AddDeviceTags(string?userId,?HashSet<string>?tags){return?SetDeviceTags(userId,?tags,?Action.AddDeviceTags);}移除標簽
public?Result?RemoveDeviceTags(string?userId,?HashSet<string>?tags){return?SetDeviceTags(userId,?tags,?Action.RemoveDeviceTags);}添加別名
public?void?AddAlias(string?registrationId,?string?alias){if?(string.IsNullOrWhiteSpace(registrationId)?||?string.IsNullOrWhiteSpace(alias))throw?new?Exception("設備標識和別名不能為空。");var?result?=?DeviceClient.UpdateDeviceInfo(registrationId,?new?DevicePayload?{?Alias?=?alias?});if?(result.StatusCode?==?HttpStatusCode.OK)return;var?errorMsg?=$"別名添加失敗,HTTP 狀態碼:{result.StatusCode}"?+$",返回信息:{result.Content}"?+$",別名:{alias},設備標識:{registrationId}";WriteErrorLog(errorMsg);}生成標簽推送信息
private?PushPayload?PushPayloadForTag(){var?notification?=?GetNotification();SetIosSoundSilent(notification);if?(Audiences.Count?>?20){Audiences.RemoveRange(20,?Audiences.Count?-?20);WriteErrorLog("消息推送標簽超過20個,自動取前20個進行推送。");}return?new?PushPayload{Platform?=?new?List<string>?{?"android",?"ios"?},Audience?=?new?{?tag?=?Audiences.ToArray()?},//"all",//Notification?=?notification,Options?=?new?Options?{?TimeToLive?=?TimeToLive,?IsApnsProduction?=?IsApnsProduction()?}};}發送消息推送
public?bool?Send(){PushPayload?payload;switch?(PushType){case?PushType.Tag:if?(Audiences?==?null?||?Audiences.Count?==?0)throw?new?Exception("推送目標集合不能為空。");payload?=?PushPayloadForTag();break;case?PushType.Alias:if?(Audiences?==?null?||?Audiences.Count?==?0)throw?new?Exception("推送目標集合不能為空。");payload?=?PushPayloadForAlias();break;case?PushType.All://?極光免費版限制了廣播次數,每天10次,現改為標簽推送代替//payload?=?PushPayloadForAll();//zsj?2021-01-19?先兼容下之前調用此處代碼沒給Category賦值的Category==?ProgramCategory.None,后續排查完后這段代碼這刪掉if?(Category?==?ProgramCategory.Basketball?||?Category?==?ProgramCategory.None)Audiences?=?new?List<string>?{?UserBll.BroadcastTagBasketball?};else?if?(Category?==?ProgramCategory.Football)Audiences?=?new?List<string>?{?UserBll.BroadcastTagFootball?};else?if?(Category?==?ProgramCategory.ESports)Audiences?=?new?List<string>?{?UserBll.BroadcastTagESports?};payload?=?PushPayloadForTag();break;default:return?false;}try{//zsj?2021-05-31?計算推送前后的時間差DateTime?dtNow?=?DateTime.Now;//??WriteErrorLog("極光開始推送,JPushClient,推送前時間:"?+?dtNow?+?";ContentId[即AssociateId]="?+?ContentId?+?";Title="?+?Title?+?";Message="?+?Message?+?";ContentTitle="?+?ContentTitle,?Xmzy.Common.Tools.Log.LogSource.ApiExecute,?"極光推送");if?(payload.Options?==?null){payload.Options?=?new?Options()?{?TimeToLive?=?60?*?60?*?3?};}else{payload.Options.TimeToLive?=?60?*?60?*?3;}if?(isdisand?==?"0"){List<string>?p?=?new?List<string>();p.Add("ios");//["android",?"ios","quickapp"]payload.Platform?=?p.ToArray();//??"platform":?"all",}var?result?=?JPushClient.SendPush(payload);DateTime?dtNow2?=?DateTime.Now;TimeSpan?ts?=?dtNow2?-?dtNow;//?WriteErrorLog("極光開始推送,JPushClient,推送后時間:"?+?dtNow2?+?";間隔相差"?+?ts.TotalSeconds?+?"秒"?+?";ContentId[即AssociateId]="?+?ContentId?+?";Title="?+?Title?+?";Message="?+?Message?+?";ContentTitle="?+?ContentTitle,?Xmzy.Common.Tools.Log.LogSource.ApiExecute,?"極光推送");_Logger.Error("payload------"?+?JsonConvert.SerializeObject(payload));_Logger.Error("result-----------"?+?JsonConvert.SerializeObject(result));if?(result.StatusCode?!=?HttpStatusCode.OK){WriteErrorLog("消息推送失敗,HTTP 狀態碼:"?+?result.StatusCode+?",返回信息:"?+?result.Content?+?"。");}return?true;}catch?(Exception?e){WriteErrorLog(e.Message);return?false;}}總結
以上是生活随笔為你收集整理的C# Jpush 极光推送消息推送的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .NET 6新特性试用 | TryGet
- 下一篇: 在 .NET Core 中如何让 Ent