public string PushMsg(string strJson)
        {
            string result = string.Empty;
            string message = string.Empty;
            try
            {
                Dictionary<string, string> dic = json.Deserialize<Dictionary<string, string>>(strJson);
                BaiduPush Bpush = new BaiduPush("POST");
                String messages = "";
                String method = "push_msg";
                TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
                uint device_type = uint.Parse(dic["deviceType"]);
                uint unixTime = (uint)ts.TotalSeconds;
                string messageksy = "pushkeys";
                string apiKey = SysFunction.GetConfigValue("ApiKey");

                if (device_type == 4)
                {
                    IOSNotification notification = new IOSNotification();
                    notification.title = dic["title"];
                    notification.description = dic["des"];
                    messages = notification.getJsonString();
                }
                else if (device_type == 3)
                {
                    AndroidNotification notification = new AndroidNotification();
                    notification.title = dic["title"];
                    notification.description = dic["des"];
                    messages = notification.getJsonString();
                }

                PushOptions pOpts;
                if (dic["pushType"].Equals("1"))
                {
                    pOpts = new PushOptions(method, apiKey, dic["userID"], dic["channelID"], device_type, messages, messageksy, unixTime);
                }
                else if (dic["pushType"].Equals("2"))
                {
                    pOpts = new PushOptions(method, apiKey, dic["tag"], device_type, messages, messageksy, unixTime);
                }
                else
                {
                    pOpts = new PushOptions(method, apiKey, device_type, messages, messageksy, unixTime);
                }

                pOpts.message_type = uint.Parse(dic["msgType"]);

                message = Bpush.PushMessage(pOpts);
                result = "0";
            }
            catch (Exception ex)
            {
                HandleException(ex, ref result, ref message);
            }
            return GetResultStr(result, message);
        }
        /// <summary>
        /// 标签操作
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <param name="tag">标签名</param>
        /// <param name="method">方法名</param>
        /// <param name="result"></param>
        /// <param name="message"></param>
        private void TagOperation(string userId, string tag, string method, ref string result, ref string message)
        {
            BaiduPush Bpush = new BaiduPush("POST");
            TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
            uint unixTime = (uint)ts.TotalSeconds;
            string apiKey = SysFunction.GetConfigValue("ApiKey");

            TagOptions tOpts = new TagOptions(method, apiKey, userId, tag, unixTime);

            message = Bpush.SetTag(tOpts);
            result = "0";
        }
        public override void OnLoad(HttpContext context)
        {
            base.OnLoad(context);
            requestBody             = new RequestBody();
            requestBody.accessToken = context.Request["accessToken"];
            requestBody.uTo         = Convert.ToInt32(context.Request["to"]);
            requestBody.content     = context.Request["content"];

            if (requestBody.content.Length == 0 || requestBody.accessToken.Trim().Length == 0 || requestBody.uTo == 0)
            {
                SystemResponse.Output(SystemResponse.TYPE_NULLPARAMETER, out statusCode, out responseJson);
            }
            else
            {
                //验证用户
                TokenHelper    token     = new TokenHelper();
                UserTokenModel fromModel = token.getUserToken(requestBody.accessToken);
                UserTokenModel toModel   = token.getUserToken(requestBody.uTo);
                if (fromModel == null)
                {
                    SystemResponse.Output(SystemResponse.TYPE_EXPIRE, out statusCode, out responseJson);
                }
                else
                {
                    int msgstatus = 0;

                    #region 入库至本地
                    ModelAdo <MsgModel> modelAdo = new ModelAdo <MsgModel>();
                    MsgModel            msg      = new MsgModel();
                    msg.ufrom      = Convert.ToInt32(fromModel.uid);
                    msg.uto        = requestBody.uTo;
                    msg.content    = requestBody.content;
                    msg.createTime = StringHelper.ConvertDateTimeInt(DateTime.Now);
                    msg.status     = msgstatus;
                    if (modelAdo.Insert(msg) >= 1)
                    {
                        #region 百度推送
                        if (toModel != null && toModel.bpuserId.Length >= 1 && toModel.channelId.Length >= 1)
                        {
                            //获取插入本地数据
                            MsgModel msgPush = modelAdo.GetModel("ufrom=?ufrom AND uto=?uto AND createTime=?createTime AND status=0", "",
                                                                 new MySqlParameter("?ufrom", msg.ufrom),
                                                                 new MySqlParameter("?uto", msg.uto),
                                                                 new MySqlParameter("?createTime", msg.createTime));
                            if (msgPush != null)
                            {
                                try
                                {
                                    //百度配置信息
                                    string secretKey    = ConfigurationManager.AppSettings["baidu_secret_key"];
                                    string apiKey       = ConfigurationManager.AppSettings["baidu_api_key"];
                                    uint   depolyStatus = uint.Parse(ConfigurationManager.AppSettings["baidu_depoly_status"]);

                                    String    messages     = "";
                                    TimeSpan  ts           = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
                                    uint      unixTime     = (uint)ts.TotalSeconds;
                                    string    messageksy   = "api";
                                    uint      message_type = 1;
                                    BaiduPush Bpush        = new BaiduPush("POST", secretKey);



                                    if (toModel.deviceType == 1)
                                    {
                                        message_type       = 1;
                                        toModel.deviceType = 4;
                                        IOSNotification notifaction = new IOSNotification();
                                        notifaction.id         = msgPush.id;
                                        notifaction.ufrom      = msgPush.ufrom;
                                        notifaction.uto        = msgPush.uto;
                                        notifaction.content    = msgPush.content.Trim();
                                        notifaction.createTime = string.Format("{0:yyyy/MM/dd HH:mm:ss}", System.DateTime.Now);
                                        notifaction.type       = msgPush.type;
                                        notifaction.status     = 1;
                                        IOSAPS aps = new IOSAPS()
                                        {
                                            alert = "收到一条新消息",
                                        };
                                        notifaction.aps = aps;
                                        messages        = notifaction.getJsonString();
                                    }
                                    else
                                    {
                                        message_type       = 0;
                                        toModel.deviceType = 3;
                                        BaiduPushNotification notifaction = new BaiduPushNotification();
                                        notifaction.title = "";
                                        //构建custom_content信息
                                        BaiduDescription bdMsg = new BaiduDescription();
                                        bdMsg.id                   = msgPush.id;
                                        bdMsg.ufrom                = msgPush.ufrom;
                                        bdMsg.uto                  = msgPush.uto;
                                        bdMsg.content              = msgPush.content;
                                        bdMsg.createTime           = string.Format("{0:yyyy/MM/dd HH:mm:ss}", System.DateTime.Now);
                                        bdMsg.type                 = msgPush.type;
                                        bdMsg.status               = 1;
                                        notifaction.description    = "收到一条新消息";
                                        notifaction.custom_content = bdMsg;
                                        messages                   = notifaction.getJsonString();
                                    }



                                    PushOptions pOpts = new PushOptions("push_msg", apiKey, toModel.bpuserId.ToString(),
                                                                        toModel.channelId.ToString(), Convert.ToUInt32(toModel.deviceType), messages, messageksy, unixTime);
                                    pOpts.message_type  = message_type;
                                    pOpts.deploy_status = depolyStatus;
                                    pOpts.push_type     = 1;
                                    string response = Bpush.PushMessage(pOpts);
                                    responseJson = response;
                                    msgstatus    = 1;

                                    //处理数据为已读
                                    if (msg.status == 0)
                                    {
                                        msgPush.status = 1;
                                        modelAdo.Update(msgPush);
                                    }

                                    //SystemResponse.Output(SystemResponse.TYPE_OK, out statusCode, out responseJson);
                                    //responseJson = strBDMsg;
                                }
                                catch (Exception ex)
                                {
                                    responseJson = ex.ToString();
                                    SystemResponse.Output(SystemResponse.TYPE_ERROR, out statusCode, out responseJson);
                                }
                            }
                            #endregion
                        }
                        else
                        {
                            SystemResponse.Output(SystemResponse.TYPE_ERROR, out statusCode, out responseJson);
                        }
                        #endregion
                    }
                }
            }
        }
Beispiel #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ApiKey"></param>
        /// <param name="SecretKey"></param>
        /// <param name="sandbox"></param>
        /// <param name="platform">1:苹果 2:安卓</param>
        /// <param name="BDUserID"></param>
        /// <param name="BDChannelID"></param>
        /// <returns></returns>
        public static string SendPush(string ApiKey, string SecretKey, bool sandbox, int platform, string BDUserID, string BDChannelID, string NoticeTitle, string NoticeContent, string custom_content)
        {
            //当前APIKEY
            string sk = SecretKey;
            //当前SecreKey
            string ak = ApiKey;

            //百度推送
            BaiduPush Bpush    = new BaiduPush("POST", sk);
            String    apiKey   = ak;
            String    messages = "";
            String    method   = "push_msg";//默认消息推送

            //当前时间戳为 PushOptions服务
            TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
            //默认设备为安卓

            /*
             * 云推送支持多种设备,各种设备的类型编号如下:
             * 3:Andriod设备;
             * 4:iOS设备;
             * 如果存在此字段,则向指定的设备类型推送消息。 默认为android设备类型。
             */
            uint device_type = platform == 1 ? (uint)4 : (uint)3;
            //获取总秒数
            uint unixTime = (uint)ts.TotalSeconds;
            //当前消息类型
            uint message_type;
            //消息签名一一对应
            string messageksy = "xxx000";

            //通知
            message_type = 1;

            //如果选择的是通知并且是苹果,那么设备类型改变为4
            if (platform == 1) //1:苹果 2:安卓
            {
                IOSNotification notification = new IOSNotification();
                notification.title       = NoticeContent;
                notification.description = NoticeTitle;
                messages = notification.getJsonString();
            }
            else
            {
                BaiduPushNotification notification = new BaiduPushNotification();
                notification.title          = NoticeTitle;
                notification.description    = NoticeContent;
                notification.custom_content = custom_content;
                messages = notification.getJsonString();
            }
            PushOptions pOpts = new PushOptions(method, apiKey, BDUserID, BDChannelID, device_type, messages, messageksy, unixTime);

            //消息或则通知
            pOpts.message_type = message_type;

            //如果是苹果发布版本
            if (platform == 1 && sandbox)
            {
                //则当前是开发状态
                pOpts.deploy_status = 1;
            }
            else if (platform == 1 && !sandbox)//如果是苹果开发版本
            {
                //则当前是生产状态
                pOpts.deploy_status = 2;
            }
            string response = Bpush.PushMessage(pOpts);

            return(response);
        }
Beispiel #5
0
        public void PostCreate(string title, string body, string receivers, string[] platform, int schoolTermID)
        {
            int      _code     = 0;
            string   _message  = "发布推送消息失败";
            Employee _employee = Session["logonUser"] as Employee;

            if (!string.IsNullOrEmpty(title) && !string.IsNullOrEmpty(body))
            {
                List <string> _platform = new List <string>(platform);

                Notification _notification = new Notification();

                _notification.Title     = title;
                _notification.Body      = body;
                _notification.Platform  = string.Join(",", platform);
                _notification.Receivers = receivers;
                _notification.STID      = schoolTermID;

                _code = _notificationService.InsertNotification(_notification);

                if (_code > 0)
                {
                    List <int> _userIDs = new List <int>();

                    string[] _receivers = receivers.Split(',');
                    if (_receivers.Length > 0)
                    {
                        IList <Employee> _employeeList = _employeeService.GetEmployeeByNames(_receivers);
                        foreach (Employee _item in _employeeList)
                        {
                            _userIDs.Add(_item.ID);
                        }
                    }

                    if (schoolTermID > 0)
                    {
                        Registration _registration = new Registration();
                        _registration.STID = schoolTermID;

                        IList <Registration> _registrationList = _registrationService.GetRegistration(_registration);
                        foreach (Registration _item in _registrationList)
                        {
                            _userIDs.Add(_item.EmployeeID.Value);
                        }
                    }

                    if (_platform.Contains("iphone"))
                    {
                        IList <DeviceToken> _deviceTokenList = _deviceTokenService.GetDeviceToken("iphone", null, _userIDs.ToArray());

                        List <PushMessage> _pushMessageList = new List <PushMessage>();
                        foreach (DeviceToken _deviceToken in _deviceTokenList)
                        {
                            PushMessage _pushMessage = new PushMessage();
                            _pushMessage.MessageID = _code;
                            _pushMessage.Token     = _deviceToken.Token;
                            _pushMessage.Platform  = "iphone";
                            _pushMessage.Body      = body;

                            _pushMessageList.Add(_pushMessage);
                        }

                        var _thread = new Thread(() =>
                        {
                            foreach (PushMessage _pushMessage in _pushMessageList)
                            {
                                ANPSPush _anpsPush = new ANPSPush();
                                _anpsPush.PushMessage(_pushMessage);
                            }
                        });
                        _thread.Start();
                    }

                    if (_platform.Contains("android"))
                    {
                        IList <DeviceToken> _deviceTokenList = _deviceTokenService.GetDeviceToken("android", null, _userIDs.ToArray());

                        List <PushMessage> _pushMessageList = new List <PushMessage>();
                        foreach (DeviceToken _deviceToken in _deviceTokenList)
                        {
                            PushMessage _pushMessage = new PushMessage();
                            _pushMessage.ClientID = _deviceToken.ClientID;
                            _pushMessage.Token    = _deviceToken.Token;
                            _pushMessage.Platform = "android";
                            _pushMessage.Body     = body;

                            _pushMessageList.Add(_pushMessage);
                        }

                        var _thread = new Thread(() =>
                        {
                            BaiduPush _baiduPush = new BaiduPush(_pushMessageList);
                            _baiduPush.PushMessage();
                        });
                        _thread.Start();
                    }

                    _message = "发布推送消息成功";
                }
                else
                {
                    _message = "对不起,发布推送消息失败,可能有重名!";
                }
            }
            else
            {
                _message = "推送消息标题不能为空!";
            }

            RenderText("{\"code\":\"" + _code + "\",\"message\":\"" + _message + "\"}");

            CancelLayout();
        }