Exemplo n.º 1
0
        /// <inheritdoc/>
        public async Task <NotificationResult> Send(ApnsNotification notification, string deviceToken, bool isVoip)
        {
            var hostUri = $"{apnsUri}{deviceToken}";
            var payload = notification.GetPayload();
            var counter = 0;

            NotificationResult result = NotificationResult.Successful();

            while (counter < 5)
            {
                HttpResponseMessage response = await SendRequest(isVoip, payload, hostUri);

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    return(NotificationResult.Successful());
                }

                result = await GetErrorResult(response);

                if (result.Error.Code != NotificationErrorCode.ExpiredAccessToken)
                {
                    return(result);
                }

                RefreshToken(DateTimeOffset.Now, true);
                counter++;
            }

            return(result);
        }
Exemplo n.º 2
0
        private async Task <NotificationResult> SendRequest(string requestBody, string authenticationKey, string uri, bool useHttpV1)
        {
            using var request = new HttpRequestMessage(HttpMethod.Post, uri);
            var headerAuthenticationKey   = useHttpV1 ? "Bearer" : "key";
            var headerAuthenticationValue = useHttpV1 ? authenticationKey : $"={authenticationKey}";

            request.Headers.Authorization = new AuthenticationHeaderValue(headerAuthenticationKey, headerAuthenticationValue);
            request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");

            var response = await client.SendAsync(request);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                return(await GetErrorResult(response));
            }

            return(NotificationResult.Successful());
        }