Exemplo n.º 1
0
        public async Task <Result <string> > RequestAsync(DeviceGroupRequestOperation operation, int userId, string token, CancellationToken cancellationToken = default)
        {
            var data = new DeviceGroupRequest {
                NotificationKeyName = userId.ToString(), RegistrationIds = new List <string> {
                    token
                }
            };

            if (operation == DeviceGroupRequestOperation.Create)
            {
                data.Operation = "create";
            }
            else
            {
                var prevToken = await FcmUserTokenRepository.GetUserTokenAsync(userId, cancellationToken);

                data.NotificationKey = prevToken;
                data.Operation       = operation == DeviceGroupRequestOperation.Remove ? "remove" : "add";
            }

            var response = await FcmApiHttpClient.Client.PostAsJsonAsync("notification", data, cancellationToken);

            if (response.IsSuccessStatusCode)
            {
                var result = await response.Content.ReadAsAsync <DeviceGroupResponse>(cancellationToken);

                return(new Result <string>(result.NotificationKey));
            }

            return(Result <string> .WithError("http", ""));
        }
Exemplo n.º 2
0
        public async Task <string> RequestAsync(DeviceGroupRequestOperation operation, int userId, string token, CancellationToken cancellationToken = default)
        {
            var data = new DeviceGroupRequest {
                NotificationKeyName = "user_" + userId,
                RegistrationIds     = new List <string> {
                    token
                }
            };

            if (operation == DeviceGroupRequestOperation.Create)
            {
                data.Operation = "create";
            }
            else
            {
                data.NotificationKey = await _fcmUserTokenRepository.GetUserTokenAsync(userId, cancellationToken);

                data.Operation = operation == DeviceGroupRequestOperation.Remove ? "remove" : "add";
            }

            _logger.LogInformation("device-group-data", data);

            var response = await _fcmApiHttpClient.Client.PostAsJsonAsync("notification", data, cancellationToken);

            if (response.IsSuccessStatusCode)
            {
                var result = await response.Content.ReadAsAsync <DeviceGroupResponse>(cancellationToken);

                return(result.NotificationKey);
            }

            var errorBody = await response.Content.ReadAsStringAsync();

            throw new Exception(errorBody);
        }