public async Task SendMessage <T>(string name, T data, CancellationToken cancellationToken)
        {
            if (!IsSessionActive)
            {
                return;
            }

            if (string.IsNullOrEmpty(_senderId) || string.IsNullOrEmpty(_applicationId))
            {
                return;
            }

            string strData = _json.SerializeToString(data);
            var    req     = new FirebaseBody
            {
                to   = _token,
                data = new FirebaseData
                {
                    msgdata = strData
                }
            };

            var byteArray = Encoding.UTF8.GetBytes(_json.SerializeToString(req));

            var enableLogging = false;

#if DEBUG
            enableLogging = true;
#endif

            var options = new HttpRequestOptions
            {
                Url = "https://fcm.googleapis.com/fcm/send",
                RequestContentType  = "application/json",
                RequestContentBytes = byteArray,
                CancellationToken   = cancellationToken,
                LogRequest          = enableLogging,
                LogResponse         = enableLogging,
                LogErrors           = enableLogging
            };

            options.RequestHeaders["Authorization"] = string.Format("key={0}", _applicationId);
            options.RequestHeaders["Sender"]        = string.Format("id={0}", _senderId);

            using (var response = await _httpClient.Post(options).ConfigureAwait(false))
            {
            }
        }
Beispiel #2
0
        public async Task SendMessage <T>(string name, string messageId, T data, ISessionController[] allControllers, CancellationToken cancellationToken)
        {
            if (!IsSessionActive)
            {
                return;
            }

            if (string.IsNullOrEmpty(_senderId) || string.IsNullOrEmpty(_applicationId))
            {
                return;
            }

            foreach (var controller in allControllers)
            {
                // Don't send if there's an active web socket connection
                if ((controller is WebSocketController) && controller.IsSessionActive)
                {
                    return;
                }
            }

            var msg = new WebSocketMessage <T>
            {
                Data        = data,
                MessageType = name,
                MessageId   = messageId,
                ServerId    = _appHost.SystemId
            };

            var req = new FirebaseBody <T>
            {
                to   = _token,
                data = msg
            };

            var byteArray = Encoding.UTF8.GetBytes(_json.SerializeToString(req));

            var enableLogging = false;

#if DEBUG
            enableLogging = true;
#endif

            var options = new HttpRequestOptions
            {
                Url = "https://fcm.googleapis.com/fcm/send",
                RequestContentType  = "application/json",
                RequestContentBytes = byteArray,
                CancellationToken   = cancellationToken,
                LogRequest          = enableLogging,
                LogResponse         = enableLogging,
                LogErrors           = enableLogging
            };

            options.RequestHeaders["Authorization"] = string.Format("key={0}", _applicationId);
            options.RequestHeaders["Sender"]        = string.Format("id={0}", _senderId);

            using (var response = await _httpClient.Post(options).ConfigureAwait(false))
            {
            }
        }