C#個推SDK推送安卓+iOS


下載個推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;action=android.intent.action.oppopush;launchFlags=0x14000000;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;action=android.intent.action.oppopush;launchFlags=0x14000000;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推送上架這一套服務很好用。

7,uniapp App.vue相關代碼 直接寫在onLaunch

       //監聽click事件,用戶從消息中心點擊觸發的
        plus.push.addEventListener(
            'click',
            function(msg) {
                //根據payload傳遞過來的數據,打開一個詳情
                var payload = msg.payload;
                if (payload) {
                    // payload 按照規范是 Object,但實際推送過來有可能是 String,需要多一步處理;
                    if (typeof payload === 'string') {
                        payload = JSON.parse(payload);
                    }
                    if (typeof payload === 'object') {
                        if (payload.url) {
                            setTimeout(function(res) {
                                uni.navigateTo({
                                    url: payload.url
                                });
                            }, 1000);
                        }
                    }
                }
            },
            false
        );

 

        //監聽receive事件
        plus.push.addEventListener(
            'receive',
            function(msg) {
                if (plus.os.name != 'iOS') {
                    plus.push.createMessage(msg.title, msg.payload);
                }
                //根據payload傳遞過來的數據,打開一個詳情
                var payload;
                if (msg.payload) {
                    //如透傳消息不符合格式,則“payload”屬性為string類型
                    //這里的示例以json字符串去解析,實際上也可以做字符串匹配
                    if (typeof msg.payload == 'string') {
                        try {
                            payload = JSON.parse(msg.payload);
                        } catch (error) {}
                    } else if (typeof msg.payload == 'object') {
                        //iOS應用正處於前台運行時收到推送,也觸發receive事件,此時payload為json對象
                        plus.push.createMessage(msg.title, msg.content);
                    }
                }
            },
            false
        );

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM