Esempio n. 1
0
        protected virtual IDictionary <string, string> GenerateParameters(Options.IAppOption app, PushingSenderSettings settings)
        {
            var center = PushingServiceCenter.GetInstance(this.GetRegion(app));

            var dictionary = new Dictionary <string, string>()
            {
                //推送方式(高级推送)
                { "Action", "Push" },

                //以下是公共参数部分
                { "Format", "JSON" },
                { "RegionId", center.Alias },
                { "Version", "2016-08-01" },
                { "AccessKeyId", this.GetCertificate(app).Name },
                { "SignatureMethod", "HMAC-SHA1" },
                { "SignatureVersion", "1.0" },
                { "SignatureNonce", ((ulong)Zongsoft.Common.Randomizer.GenerateInt64()).ToString() },
                { "Timestamp", Utility.GetTimestamp() },

                //以下是推送接口特定参数
                { "AppKey", app.Code },
                { "PushType", settings.Type.ToString().ToUpperInvariant() },
                { "DeviceType", settings.DeviceType.ToString().ToUpperInvariant() },
                { "Target", settings.TargetType.ToString().ToUpperInvariant() },
            };

            if (settings.Expiry > 0)
            {
                dictionary["StoreOffline"] = "true";
                dictionary["ExpireTime"]   = DateTime.UtcNow.AddMinutes(settings.Expiry).ToString("s") + "Z";
            }

            return(dictionary);
        }
Esempio n. 2
0
        /// <summary>
        /// 发送消息或通知到移动设备。
        /// </summary>
        /// <param name="name">指定的消息推送应用的名字。</param>
        /// <param name="title">指定的消息或通知的标题。</param>
        /// <param name="content">指定的消息或通知的内容。</param>
        /// <param name="destination">指定的推送目标。</param>
        /// <param name="settings">指定的推送设置参数。</param>
        /// <returns>返回推送的结果。</returns>
        public async Task <PushingResult> SendAsync(string name, string title, string content, string destination, PushingSenderSettings settings)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (string.IsNullOrWhiteSpace(destination))
            {
                throw new ArgumentNullException(nameof(destination));
            }

            if (string.IsNullOrWhiteSpace(content))
            {
                return(null);
            }

            //确认移动推送的配置
            var configuration = this.EnsureConfiguration();

            //获取指定名称的短信模板配置,如果获取失败则抛出异常
            if (!configuration.Apps.TryGet(name, out var app))
            {
                throw new InvalidOperationException($"The specified '{name}' app is not existed.");
            }

            //获取当前短信模板关联的凭证
            var certificate = this.GetCertificate(app);

            //生成请求的查询参数集
            var parameters = this.GenerateParameters(app, settings);

            parameters["TargetValue"] = destination;
            parameters["Title"]       = string.IsNullOrWhiteSpace(title) ? "New " + settings.Type.ToString() : title;
            parameters["Body"]        = content;

            //获取当前应用所在的服务区域
            var center = PushingServiceCenter.GetInstance(this.GetRegion(app));

            //构建移动推送的HTTP请求消息包
            var request = new HttpRequestMessage(HttpMethod.Get, "http://" + center.Path + "?" + Utility.GetQueryString(parameters));

            request.Headers.Accept.TryParseAdd("application/json");

            //获取当前实例关联的HTTP客户端程序
            var http = this.GetHttpClient(certificate);

            //提交移动推送请求
            var response = await http.SendAsync(request);

            return(await this.GetResultAsync(response.Content));
        }