C#個推SDK推送安卓+iOS

来源:https://www.cnblogs.com/chenmengmeng/archive/2020/02/25/12360390.html
-Advertisement-
Play Games

下載個推SDK,找到這兩個dll直接引用。 using引用 using com.gexin.rp.sdk.dto; using com.igetui.api.openservice; using com.igetui.api.openservice.igetui; using com.igetui. ...


下載個推SDK,找到這兩個dll直接引用。

using引用

using com.gexin.rp.sdk.dto;
using com.igetui.api.openservice;
using com.igetui.api.openservice.igetui;
using com.igetui.api.openservice.igetui.template;
using com.igetui.api.openservice.igetui.template.notify;
using com.igetui.api.openservice.payload;

 

兩種方案獲取到這些參數。

public const string HOST = "http://sdk.open.api.igexin.com/apiex.htm";
public const string APPID = "xxxxxxxxxxxxx";
public const string APPKEY = "xxxxxxxxxxxxx";
public const string AppSecret = "xxxxxxxxxxxxx";
public const string MASTERSECRET = "xxxxxxxxxxxxx";

 

1,使用unipush   https://dev.dcloud.net.cn/uni/push  在unipush裡面申請一個帳號,開通推送就能得到這些參數。

2,去個推註冊並且配置相關參數 

說明:UniPush由DCloud與個推聯合打造。AppSecret和MasterSecret由個推保存,DCloud並不保存。個推是A股上市公司,開發者可放心使用UniPush業務

unipush並不是專門為uniapp所使用,可以單獨使用unipush功能,其相關配置和操作頁面個人感覺比個推的好用。

透傳頁面使用,相關參數說明一目瞭然。

 

 

1.1配置 推送需要2步,配置應用平臺。

 

1.2 配置安卓廠商通道 

 

 

 

 

 

2 推送方法  

2.1推送單個用戶
        /// <summary>
        /// 推送單個用戶
        /// </summary>
        /// <param name="title">標題 例如 迪信通  搶購會</param>
        /// <param name="content">內容 例如 華為Mate30 5G搶購</param>
        /// <param name="url">APP跳轉地址 商品單頁 活動頁 或者其它頁面</param>
        /// <param name="cid">資料庫pushclientid欄位</param> 
        /// <returns>推送結果</returns>
        public static string PushMessageToSingle(string title, string content, string url, string cid)
        {
            IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET);
            TransmissionTemplate template = TransmissionTemplateAndroidiOS(title, content, url);
            //單推消息模型
            SingleMessage message = new SingleMessage();
            //當用戶不線上 是否離線存儲
            message.IsOffline = true;
            //離線有效時間
            message.OfflineExpireTime = 1000 * 3600 * 12;
            message.Data = template;
            //當前網路 1wifi 2-234G 0不限制
            message.PushNetWorkType = 0;
            com.igetui.api.openservice.igetui.Target target = new
com.igetui.api.openservice.igetui.Target();

            target.appId = APPID;
            target.clientId = cid;

            String pushResult = push.pushMessageToSingle(message, target);

            return pushResult;
        }

 

2.2 推送一批用戶

        /// <summary>
        /// 推送一批用戶
        /// </summary>
        /// <param name="title">標題 例如  搶購會</param>
        /// <param name="content">內容 例如 華為Mate30 5G搶購 </param>
        /// <param name="url">APP跳轉地址 商品單頁 活動頁 或者其它頁面</param>
        /// <param name="cids">資料庫pushclientid欄位集合</param> 
        /// <returns>推送結果</returns>
        public static string pushMessageToList(string title, string content, string url, string[] cids)
        {
            IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET);
            ListMessage message = new ListMessage();
            NotificationTemplate template = NotificationTemplateAndroidiOS(title, content, url);
            message.IsOffline = true;
            message.OfflineExpireTime = 1000 * 3600 * 12;
            message.Data = template;
            message.PushNetWorkType = 0;
            List<com.igetui.api.openservice.igetui.Target> targetList = new
List<com.igetui.api.openservice.igetui.Target>();

            for (int i = 0; i < cids.Length; i++)
            {
                com.igetui.api.openservice.igetui.Target target1 = new
           com.igetui.api.openservice.igetui.Target();
                target1.appId = APPID;
                target1.clientId = cids[i];
                targetList.Add(target1);
            }
            String contentId = push.getContentId(message);
            String pushResult = push.pushMessageToList(contentId, targetList);
            return pushResult;
        }

 

 2.3 根據條件推送到某些條件用戶

        /// <summary>
        /// 根據條件推送到某些條件用戶
        /// </summary>
        /// <param name="title">標題 例如  搶購會</param>
        /// <param name="content">內容 例如 華為Mate30 5G搶購</param>
        /// <param name="url">APP跳轉地址 商品單頁 活動頁 或者其它頁面</param>
        /// <param name="provinces">省份s  北京_上海_河南  預設不傳</param>
        /// <param name="platform">ANDROID   IOS  ALL   3種值  預設ALL不傳</param>
        /// <returns>推送結果</returns>
        public static string pushMessageToApp(string title, string content, string url, string provinces = "", string platform = "ALL")
        {
            IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET);
            AppMessage message = new AppMessage();
            message.Speed = 100;
            TransmissionTemplate template = TransmissionTemplateAndroidiOS(title, content, url);
            message.IsOffline = true;
            message.OfflineExpireTime = 1000 * 3600 * 12;
            message.Data = template;
            message.PushNetWorkType = 0;
            List<String> appIdList = new List<string>();
            appIdList.Add(APPID);
            //手機操作系統類型
            List<String> phoneTypeList = new List<string>();
            if (platform == "ALL")
            {
                phoneTypeList.Add("ANDROID");
                phoneTypeList.Add("IOS");
            }
            else if (platform == "ANDROID")
            {
                phoneTypeList.Add("ANDROID");
            }
            else if (platform == "IOS")
            {
                phoneTypeList.Add("IOS");
            }

            //地址
            List<String> provinceList = new List<string>();

            if (provinces.IsNotNullOrEmpty())
            {
                string[] provincesList = provinces.Split(new[] { '_' }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < provincesList.Length; i++)
                {
                    provinceList.Add(provincesList[i]);
                }
            }

            //標簽
            List<String> tagList = new List<string>();

            message.AppIdList = appIdList;
            message.PhoneTypeList = phoneTypeList;
            message.ProvinceList = provinceList;
            message.TagList = tagList;

            String pushResult = push.pushMessageToApp(message);
            return pushResult;
        }

 

3.1

模版一

        /// <summary>
        /// 模版一
        /// </summary>
        /// <param name="title">標題</param>
        /// <param name="content">內容</param>
        /// <param name="url">鏈接 APP中要跳轉的頁面</param>
        /// <returns></returns>
        public static NotificationTemplate NotificationTemplateAndroidiOS(string title, string content, string url)
        {
            NotificationTemplate template = new NotificationTemplate();
            template.AppId = APPID;
            template.AppKey = APPKEY;
            template.Title = title;
            template.Text = content;
            template.Logo = "";
            template.LogoURL = "";
            template.TransmissionType = 1;
            template.TransmissionContent = "{\"url\":\"" + url + "\"}";
            template.IsRing = true;
            template.IsVibrate = true;
            template.IsClearable = true;

            //安卓透傳廠商通道
            Notify notify = new Notify();
            notify.Content = title;
            notify.Title = content;
            string newUrl = "{\"url\":\"" + url + "\"}";
            notify.Intent = $"intent:#Intent;component=您的安卓包名/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title={title};S.content={content};S.payload={newUrl};end";
            notify.Type = NotifyInfo.Types.Type._intent;
            template.set3rdNotifyInfo(notify);


            //蘋果透傳配置
            APNPayload apnpayload = new APNPayload();
            DictionaryAlertMsg alertMsg = new DictionaryAlertMsg();
            // IOS 的body用這個
            alertMsg.Body = content;
            alertMsg.ActionLocKey = "ActionLocKey";
            alertMsg.LocKey = "LocKey";
            alertMsg.addLocArg("LocArg");
            alertMsg.LaunchImage = "LaunchImage";
            //iOS8.2支持欄位
            alertMsg.Title = title;
            alertMsg.TitleLocKey = "TitleLocKey";
            alertMsg.addTitleLocArg("TitleLocArg");

            apnpayload.AlertMsg = alertMsg;
            //apnpayload.Badge = 0  +1;
            apnpayload.ContentAvailable = 0;
            apnpayload.Sound = "default";
            apnpayload.addCustomMsg("payload", "{\"url\":\"" + url + "\"}");

            template.setAPNInfo(apnpayload);



            string begin = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            string end = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss");
            template.setDuration(begin, end);
            return template;
        }


3.2

 

        /// <summary>
        /// 模版二
        /// </summary>
        /// <param name="title">標題</param>
        /// <param name="content">內容</param>
        /// <param name="url">鏈接</param>
        /// <returns></returns>
        public static TransmissionTemplate TransmissionTemplateAndroidiOS(string title, string content, string url)
        {
            TransmissionTemplate template = new TransmissionTemplate();
            template.AppId = APPID;
            template.AppKey = APPKEY;
            //應用啟動類型,1:強制應用啟動 2:等待應用啟動
            template.TransmissionType = 1;
            //透傳內容  
            template.TransmissionContent = "{\"url\":\"" + url + "\"}";

            //安卓透傳廠商通道
            Notify notify = new Notify();
            notify.Content = title;
            notify.Title = content;
            string newUrl = "{\"url\":\"" + url + "\"}";
            notify.Intent = $"intent:#Intent;component=您的安卓包名/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title={title};S.content={content};S.payload={newUrl};end";
            notify.Type = NotifyInfo.Types.Type._intent;
            template.set3rdNotifyInfo(notify);

            //蘋果透傳配置
            APNPayload apnpayload = new APNPayload();
            DictionaryAlertMsg alertMsg = new DictionaryAlertMsg();
            // IOS 的body用這個
            alertMsg.Body = content;
            alertMsg.ActionLocKey = "ActionLocKey";
            alertMsg.LocKey = "LocKey";
            alertMsg.addLocArg("LocArg");
            alertMsg.LaunchImage = "LaunchImage";
            //iOS8.2支持欄位
            alertMsg.Title = title;
            alertMsg.TitleLocKey = "TitleLocKey";
            alertMsg.addTitleLocArg("TitleLocArg");

            apnpayload.AlertMsg = alertMsg;
            //apnpayload.Badge = 0  +1;
            apnpayload.ContentAvailable = 0;
            apnpayload.Sound = "default";
            apnpayload.addCustomMsg("payload", "{\"url\":\"" + url + "\"}");

            template.setAPNInfo(apnpayload);

            string begin = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            string end = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss");
            template.setDuration(begin, end);

            return template;
        }

 

4.調用

        //調用案例
        //string result = UniPush.PushMessageToSingle("通知", "華為Mate30 5G搶購", "/pages/product/product?pid=9871&cid=288", "cid", "20200221");
        //string[] cids = { "cid" };
        //string result = UniPush.pushMessageToList("通知", "華為搶購", "/pages/product/product?pid=10019&cid=288", cids, "20200221");
        //string result = UniPush.pushMessageToApp("通知", "華為Mate30 5G搶購", "/pages/product/product?pid=9871&cid=288", "", "ALL"); 

 

5,測試結果

1,測試單推安卓APP。線上狀態:無須透傳秒到。 離線狀態:看心情1秒-15分鐘我都碰到過。

2,測試單推iOS APP。線上狀態:無須透傳秒到。 離線狀態:APNs基本做到1-5秒到。

3,測試推集合,情況和1、2相同。

4,測試推全部,1的情況好一些、2的情況不變。

 

6,總結

國內安卓推送是一個混亂的市場,每個廠商的透傳通道推送的效率各不相同,上架也比較多繁瑣。iOS推送上架這一套服務很好用。


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 面向對象程式設計(Object-Oriented Programming,OOP)是一種程式設計架構,同時也是一種程式開發的方法。對象指的是類的實例,它將對象作為程式的基本單元,將程式和數據封裝其中,以提高代碼的重用性、靈活性和擴展性。 1.1 面向對象概念 時間是由什麼組成的?現實世界是由一個一個 ...
  • public static int PostFile(string getUrl, CookieContainer cookieContainer, HttpHeader header, string postdata, byte[] postdatabyte, Stream sm) { Strea ...
  • 現在在項目里大多都是直接使用微軟的依賴註入框架,而微軟的註入方式比較簡單,不如 AutoFac 使用起來靈活,於是想給微軟的依賴註入增加一些擴展,使得可以像AutoFac 一樣比較靈活的註冊服務 ...
  • 使用Aspose組件導出 Aspose有Aspose.Slides.dll,可以無需安裝office,進行讀寫PPT文件。 Aspose可能通過Aspose.Slides.NET安裝 簡單的導出圖片demo,如下: 1 internal class PptToImagesConverter 2 { ...
  • 作為初學者來說,在C#中使用API確是一件令人頭疼的問題。 在使用API之前你必須知道如何在C#中使用結構、類型轉換、安全/不安全代碼,可控/不可控代碼等許多知識。 在.Net Framework SDK文檔中,關於調用Windows API的指示比較零散,並且其中稍全面一點的是針對Visual B ...
  • 一、前言 接上一篇《asp.net core 3.x 授權中的概念》,本篇看看asp.net core預設授權的流程。從兩個方面來看整個授權系統是怎麼運行的:啟動階段的配置、請求階段中間件的處理流程。 由於asp.net core 3.x目前使用終結點路由,因此授權框架可以用於所有asp.net w ...
  • 前言 本章將介紹客戶端緩存將介紹瀏覽器緩存和服務端緩存,使用瀏覽器緩存將減少對web伺服器的請求次數,同時可以提升性能,避免重覆的運算浪費。 ASP.NET Core對於HTTP緩存分為兩種: 客戶端(瀏覽器緩存) 服務端緩存 客戶端緩存 通過設置HTTP的響應頭 Cache Control 來完成 ...
  • 今天在iis伺服器配置asp.net網站,遇到一個問題,記錄一下: 問題:由於擴展配置問題而無法提供您請求的頁面。如果該頁面是腳本,請添加處理程式。如果應下載文件,請添加 MIME 映射。 WindowServer2012伺服器,添加角色安裝完.netframework和iis之後,運行aspx頁面 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...