Beispiel #1
0
        /// <summary>
        /// 创建单播的消息(消息类型=通知)推送请求
        /// </summary>
        /// <param name="pType">类型:1-直接发送,其它放入数据库轮循</param>
        /// <param name="pChannelID">对应MessageChannel表中的ID</param>
        /// <param name="pBaiduUserID">百度用户ID</param>
        /// <param name="pBaiduChannelID">百度通道ID</param>
        /// <param name="pMessage">百度推送的对象</param>
        /// <returns></returns>
        public static PushRequest CreateAndroidUnicastNotificationRequest(int pType, int pChannelID, string pBaiduUserID
                                                                          , string pBaiduChannelID
                                                                          , BaiduPushNotification pMessage)
        {
            PushRequest mes = new PushRequest();

            mes.PlatForm  = 1;
            mes.Type      = pType;
            mes.ChannelID = pChannelID;
            mes.Request   = PushMsgRequestBuilder.CreateUnicastNotificationRequest(pBaiduUserID, pBaiduChannelID, pMessage).ToJSON();
            return(mes);
        }
Beispiel #2
0
        private void pushMsg(SeatManage.JsonModel.JM_SeatNotice notice)
        {
            AppUserInfo app_user = AMS.ServiceProxy.App_UserInfoProxy.GetAppUserInfoByCardNoAndSchoolNum(notice.CardNo, notice.SchoolNum);
            //TODO:根据学号获取channelId。
            BaiduPushNotification notification = new BaiduPushNotification();

            notification.title                    = notice.Title;
            notification.description              = notice.Context;
            notification.notification_builder_id  = 0;
            notification.notification_basic_style = 7;
            notification.custom_content           = new Custom_content();
            string msg = notification.getJsonString();

            PushOptions pOpts = new PushOptions(app_user.ChannelId, 1, msg, null, api_key, getTimestamp(), null, Device_Type.Android);

            BaiduPush.BaiduPush push = new BaiduPush.BaiduPush("POST", secret_key);
            push.PushSingleDevice(pOpts);
        }
Beispiel #3
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);
        }
        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
                    }
                }
            }
        }