C#个推SDK推送安卓+iOS

C#个推SDK推送安卓+iOS
下载个推SDK,找到这两个dll直接引用。
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推送上架这一套服务很好用。

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
        );
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值