コード例 #1
0
        public async Task <DeviceAddResult> AddAsync([NotNull] DeviceAddOptions options, [NotNull] string appName = ElectOneSignalConstants.DefaultAppName)
        {
            var appInfo = Options.Apps.Single(x => x.AppName == appName);

            options.AppId = appInfo.AppId;

            try
            {
                var result =
                    await ElectOneSignalConstants.DefaultApiUrl
                    .ConfigureRequest(config =>
                {
                    config.JsonSerializer = ElectOneSignalConstants.NewtonsoftJsonSerializer;
                })
                    .AppendPathSegment("players")
                    .WithHeader("Authorization", $"Basic {appInfo.ApiKey}")
                    .PostJsonAsync(options)
                    .ReceiveJson <DeviceAddResult>()
                    .ConfigureAwait(true);

                return(result);
            }
            catch (FlurlHttpException e)
            {
                var response = await e.GetResponseStringAsync().ConfigureAwait(true);

                throw new HttpRequestException(response);
            }
        }
        /// <summary>
        /// Adds new device into OneSignal App.
        /// </summary>
        /// <param name="options">Here you can specify options used to add new device.</param>
        /// <returns>Result of device add operation.</returns>
        public DeviceAddResult Add(DeviceAddOptions options)
        {
            var restRequest = new RestRequest("players", Method.POST);

            restRequest.AddHeader("Authorization", string.Format("Basic {0}", base.ApiKey));

            restRequest.RequestFormat  = DataFormat.Json;
            restRequest.JsonSerializer = new NewtonsoftJsonSerializer();
            restRequest.AddBody(options);

            var restResponse = base.RestClient.Execute <DeviceAddResult>(restRequest);

            if (restResponse.ErrorException != null)
            {
                throw restResponse.ErrorException;
            }

            return(restResponse.Data);
        }
コード例 #3
0
        public async Task <DeviceAddResult> Post([FromBody] JObject objData)
        {
            UsuarioDeviceOneSignal usuarioDevice = objData.ToObject <UsuarioDeviceOneSignal>();

            //OneSignalClient clientOnesignal = new OneSignalClient(_oneSignalSettings.RestKey, _oneSignalSettings.RestUrl);


            DeviceAddOptions optionDevide = usuarioDevice.Device;

            try
            {
                optionDevide.AppId = new Guid(_oneSignalSettings.ApiKey);

                DeviceAddResult resultAdd = OneSignalDeviceAdd(optionDevide);


                if (resultAdd.IsSuccess == true)
                {
                    //Consulta os dados do usuario para associar o Token do OneSignal
                    var filter = Builders <Usuario> .Filter.Eq("_id", usuarioDevice.Usuario._id);

                    var usuario = _repository.usuarios.Find(filter).FirstOrDefaultAsync();

                    var update = Builders <Usuario> .Update
                                 .Set(x => x.TokenOneSignal, resultAdd.Id);

                    //Atualiza o Token do usuario
                    await _repository.usuarios.UpdateOneAsync(filter, update);
                }

                return(resultAdd);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
コード例 #4
0
        private DeviceAddResult OneSignalDeviceAdd(DeviceAddOptions options)
        {
            var restRequest = new RestRequest("players", Method.POST);

            restRequest.AddHeader("Content-Type", "application/json");
            restRequest.AddHeader("Authorization", string.Format("Basic {0}", _oneSignalSettings.RestKey));

            restRequest.RequestFormat  = DataFormat.Json;
            restRequest.JsonSerializer = new NewtonsoftJsonSerializer();
            restRequest.AddBody(options);

            RestClient restClient = new RestClient(_oneSignalSettings.RestUrl);

            restClient.Timeout = -1;

            var restResponse = restClient.Execute <DeviceAddResult>(restRequest);

            if (restResponse.ErrorException != null)
            {
                throw restResponse.ErrorException;
            }

            return(restResponse.Data);
        }