Пример #1
0
        public static FcmMessage SetTopicAndNotification(string topic, AndroidNotification notification)
        {
            if (string.IsNullOrWhiteSpace(notification.Color))
            {
                notification.Color = "ff7f7f7f";
            }

            var fcmMessage = new FcmMessage();

            fcmMessage.Message            = new Dictionary <string, object>();
            fcmMessage.Message["topic"]   = topic;
            fcmMessage.Message["android"] = new AndroidConfig {
                Notification = notification
            };

            return(fcmMessage);
        }
Пример #2
0
        public async Task <HttpResponse> RequestAsync(string token, AndroidNotification notification)
        {
            using (await _semaphoreLock.LockAsync())
            {
                var now = DateTime.UtcNow;

                if (_accessToken == null || now > _accessTokenExpireTime)
                {
                    _accessToken = await _googleCredential.UnderlyingCredential.GetAccessTokenForRequestAsync();

                    _accessTokenExpireTime = now + _accessTokenLifeTime;

                    var handler = new SocketsHttpHandler
                    {
                        MaxConnectionsPerServer = int.MaxValue,
                        SslOptions = new SslClientAuthenticationOptions {
                            EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls
                        }
                    };

                    _client                       = new HttpClient(handler, true);
                    _client.BaseAddress           = _baseUri;
                    _client.DefaultRequestVersion = new System.Version(2, 0);
                    _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken);
                    _client.DefaultRequestHeaders.Connection.Add("keep-alive");
                }
            }

            var json = FcmMessage.SetTokenAndData(token, notification).ToJson();

            using var res = await _client.PostAsync("", new StringContent (json, Encoding.UTF8, "application/json"));

            return(new HttpResponse
            {
                StatusCode = res.StatusCode,
                Reason = res.ReasonPhrase
            });
        }