zoukankan      html  css  js  c++  java
  • hbuilder APP服务器端(C#)推送

     实现推送有多种方法和技术手段,我这边是使用三方“个推”去实现对特定用户的推送。我自己是关联业务,对下一步任务代办人进行消息通知。

     

    1 、个推账号申请和配置

      1.1、IOS需要推送证书 参考网址:http://www.applicationloader.net/blog/zh/397.html

      1.2 、"Hbuilder”“ 个推”配置参考:http://ask.dcloud.net.cn/article/34

    2、代码处理

          2.1 C#后端

      

      private static String HOST = "http://sdk.open.api.igexin.com/apiex.htm";
            //https的域名
            //private static String HOST = "https://api.getui.com/apiex.htm";
            private static String APPID = ConfigurationManager.AppSettings["TSAPPID"].ToString();//"djNkkiQUDN7fmNwS0Lhr1";
            private static String APPKEY = ConfigurationManager.AppSettings["TSAPPKEY"].ToString();//"FMxsRBtmx1As7wHtaPeb43";
            private static String MASTERSECRET = ConfigurationManager.AppSettings["TSMASTERSECRET"].ToString();//"3KKqkvwLzW8zrLmCvNC0S7";
    
    
            public static void GetClientid(string code, string title, string text)
            {
                WriteLog("*****************************APP信息推送*****************************************");
                WriteLog("用户:" + code);
                WriteLog(title + "内容:" + text);
    
                using (var db = new DbBase())
                {
                    string sql = string.Format("select top 1 code,clientid,billDate,company from SYSTEM_PUSH_ALIAS where code='{0}'  order by billDate desc", code);
    
                    var dt = RepositoryBase.ExecuteDataTable(db, sql);
                    if (dt.Rows.Count > 0)
                    {
                        foreach (DataRow item in dt.Rows)
                        {
                            PushMessage(item["clientid"].ToString(), title, text);
                        }
                    }
                    else
                    {
    
                        WriteLog("用户:" + code + " 无对应clientid");
                    }
                }
            }
    
            public static object PushMessage(string CLIENTID, string title, string text)
            {
                return JObject.Parse(PushMessageToSingle(CLIENTID, title, text));
            }
    
            /// <summary>
            /// 用户对于的 推送CLIENTID
            /// </summary>
            /// <param name="CLIENTID"></param>
            private static string PushMessageToSingle(string CLIENTID, string title, string text)
            {
    
                IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET);
    
    
                TransmissionTemplate template = TransmissionTemplateDemo(title, text);
    
                // 单推消息模型
                SingleMessage message = new SingleMessage();
                message.IsOffline = true;                         // 用户当前不在线时,是否离线存储,可选
                message.OfflineExpireTime = 1000 * 3600 * 12;            // 离线有效时间,单位为毫秒,可选
                message.Data = template;
                //判断是否客户端是否wifi环境下推送,2为4G/3G/2G,1为在WIFI环境下,0为不限制环境
                message.PushNetWorkType = 0;
    
                com.igetui.api.openservice.igetui.Target target = new com.igetui.api.openservice.igetui.Target();
                target.appId = APPID;
                target.clientId = CLIENTID;
                //target.alias = ALIAS;
                try
                {
                    String pushResult = push.pushMessageToSingle(message, target);
    
    
                    WriteLog("-----------------------------------------------");
                    WriteLog("----------------服务端返回结果:" + pushResult);
                    return pushResult;
                }
                catch (RequestException e)
                {
                    String requestId = e.RequestId;
                    //发送失败后的重发
                    String pushResult = push.pushMessageToSingle(message, target, requestId);
    
                    WriteLog("-----------------------------------------------");
                    WriteLog("----------------服务端返回结果:" + pushResult);
                    return pushResult;
                }
            }
    
    
            //通知透传模板动作内容
            public static NotificationTemplate NotificationTemplateDemo(string title, string text)
            {
    
                NotificationTemplate template = new NotificationTemplate();
                template.AppId = APPID;
                template.AppKey = APPKEY;
                //通知栏标题
                template.Title = title;
                //通知栏内容     
                template.Text = text;
                //通知栏显示本地图片
                template.Logo = "";
                //通知栏显示网络图标
                template.LogoURL = "";
                //应用启动类型,1:强制应用启动  2:等待应用启动
                template.TransmissionType = "1";
                //透传内容  
                template.TransmissionContent = text;
                //接收到消息是否响铃,true:响铃 false:不响铃   
                template.IsRing = true;
                //接收到消息是否震动,true:震动 false:不震动   
                template.IsVibrate = true;
                //接收到消息是否可清除,true:可清除 false:不可清除    
                template.IsClearable = true;
    
                //APNPayload apnpayload = new APNPayload();
                //SimpleAlertMsg alertMsg = new SimpleAlertMsg(text);            
                //apnpayload.AlertMsg = alertMsg;
                //// apnpayload.Badge = 11;
                //apnpayload.ContentAvailable = 1;
                //apnpayload.addCustomMsg("payload", "payload");
    
                //APN高级推送
                APNPayload apnpayload = new APNPayload();
                DictionaryAlertMsg alertMsg = new DictionaryAlertMsg();
                alertMsg.Body = text;
                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 = 10;
                apnpayload.ContentAvailable = 1;
                //apnpayload.Category = "";
                //apnpayload.Sound = "test1.wav";
                apnpayload.addCustomMsg("payload", "payload");
               
                template.setAPNInfo(apnpayload);
    
                template.setAPNInfo(apnpayload);
                //设置通知定时展示时间,结束时间与开始时间相差需大于6分钟,消息推送后,客户端将在指定时间差内展示消息(误差6分钟)
                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;
            }
    
            //透传模板动作内容
            public static TransmissionTemplate TransmissionTemplateDemo(string title, string text)
            {
                TransmissionTemplate template = new TransmissionTemplate();
                template.AppId = APPID;
                template.AppKey = APPKEY;
                //应用启动类型,1:强制应用启动 2:等待应用启动
                template.TransmissionType = "2";           
                //透传内容  
                template.TransmissionContent = "{"title":"" + title + "","content":"" + text + "","payload":"payload"}";
                //设置通知定时展示时间,结束时间与开始时间相差需大于6分钟,消息推送后,客户端将在指定时间差内展示消息(误差6分钟)
                //设置客户端展示时间
    
                //iOS简单推送
                APNPayload apnpayload = new APNPayload();
                SimpleAlertMsg alertMsg = new SimpleAlertMsg(text);
                apnpayload.AlertMsg = alertMsg;
                // apnpayload.Badge = 11;
                apnpayload.ContentAvailable = 1;
                //apnpayload.Category = "";
                apnpayload.Sound = "default";
                apnpayload.addCustomMsg("payload", "payload");
                template.setAPNInfo(apnpayload);
                //APN高级推送
                //APNPayload apnpayload = new APNPayload();
                //DictionaryAlertMsg alertMsg = new DictionaryAlertMsg();
                //apnpayload.ContentAvailable = 1;
                //alertMsg.Body = "您有订单需要审核";
                //alertMsg.ActionLocKey = "ActionLocKey";
                //alertMsg.LocKey = "LocKey";
                //alertMsg.addLocArg("LocArg");
                //alertMsg.LaunchImage = "LaunchImage";
                //iOS8.2支持字段
                //alertMsg.Title = "订单提醒";
                //alertMsg.TitleLocKey = "TitleLocKey";
                //alertMsg.addTitleLocArg("TitleLocArg");
    
    
                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;
            }
    
            private static void WriteLog(string log)
            {
                CommonHelper.WriteLog(log, "_pushLog.txt");
            }

          2.2 hbuilder app端代码:

        登录后必须给每个用户绑定一个“clientId”,此ID要存储在服务器。在服务器给APP推送消息的时候需要更加用户信息找到“clientId”:

          

    ClientInfo =plus.push.getClientInfo();	
    mui.getJSON(Url,{code:user.code,cli:ClientInfo.clientid},function(json){
    		
        });
        
    

      

        IOS 应用在前台的时候,在服务器推送消息APP端是没有提示的。需要在代码上做些处理。在登录后的第一个页面添加西门监听事件:

    if (mui.os.ios) {
    			mui.plusReady(function(){				
        			plus.push.addEventListener("receive", function( msg ) {
            			if (msg.payload !=null && msg.payload!="") {          				
                			plus.push.createMessage(msg.content)
            			} 
        			}, false );
    				
    			});
    			
    		}
    

      

        

      

  • 相关阅读:
    正则元字符总表
    Request中的各种地址
    JSONP实例
    【Kindeditor编辑器】 文件上传、空间管理
    【加密算法】SHA
    【加密算法】DES
    【加密算法】3DES
    【加密算法】Base64
    【加密算法】MD5
    【加密算法】AES
  • 原文地址:https://www.cnblogs.com/jiezi/p/9565530.html
Copyright © 2011-2022 走看看