예제 #1
0
        private void ExcludeCurrentContext(PushSendRequestModel request)
        {
            var currentContext = _httpContextAccessor?.HttpContext?.
                                 RequestServices.GetService(typeof(CurrentContext)) as CurrentContext;

            if (!string.IsNullOrWhiteSpace(currentContext?.DeviceIdentifier))
            {
                request.Identifier = currentContext.DeviceIdentifier;
            }
        }
        private async Task SendPayloadToOrganizationAsync(Guid orgId, PushType type, object payload, bool excludeCurrentContext)
        {
            var request = new PushSendRequestModel
            {
                OrganizationId = orgId.ToString(),
                Type           = type,
                Payload        = payload
            };

            await AddCurrentContextAsync(request, excludeCurrentContext);
            await SendAsync(HttpMethod.Post, "push/send", request);
        }
예제 #3
0
        public async Task PostSend([FromBody] PushSendRequestModel model)
        {
            CheckUsage();

            if (!string.IsNullOrWhiteSpace(model.UserId))
            {
                await _pushNotificationService.SendPayloadToUserAsync(Prefix(model.UserId),
                                                                      model.Type.Value, model.Payload, Prefix(model.Identifier));
            }
            else if (!string.IsNullOrWhiteSpace(model.OrganizationId))
            {
                await _pushNotificationService.SendPayloadToOrganizationAsync(Prefix(model.OrganizationId),
                                                                              model.Type.Value, model.Payload, Prefix(model.Identifier));
            }
        }
예제 #4
0
        private async Task SendPayloadToUserAsync(Guid userId, PushType type, object payload, bool excludeCurrentContext)
        {
            var request = new PushSendRequestModel
            {
                UserId  = userId.ToString(),
                Type    = type,
                Payload = payload
            };

            if (excludeCurrentContext)
            {
                ExcludeCurrentContext(request);
            }

            await SendAsync(HttpMethod.Post, "push/send", request);
        }
예제 #5
0
        private async Task SendPayloadToOrganizationAsync(Guid orgId, PushType type, object payload, bool excludeCurrentContext)
        {
            var request = new PushSendRequestModel
            {
                OrganizationId = orgId.ToString(),
                Type           = type,
                Payload        = payload
            };

            if (excludeCurrentContext)
            {
                ExcludeCurrentContext(request);
            }

            await SendAsync(request);
        }
        private async Task AddCurrentContextAsync(PushSendRequestModel request, bool addIdentifier)
        {
            var currentContext = _httpContextAccessor?.HttpContext?.
                                 RequestServices.GetService(typeof(ICurrentContext)) as ICurrentContext;

            if (!string.IsNullOrWhiteSpace(currentContext?.DeviceIdentifier))
            {
                var device = await _deviceRepository.GetByIdentifierAsync(currentContext.DeviceIdentifier);

                if (device != null)
                {
                    request.DeviceId = device.Id.ToString();
                }
                if (addIdentifier)
                {
                    request.Identifier = currentContext.DeviceIdentifier;
                }
            }
        }
예제 #7
0
        private async Task SendAsync(PushSendRequestModel requestModel)
        {
            var tokenStateResponse = await HandleTokenStateAsync();

            if (!tokenStateResponse)
            {
                return;
            }

            var message = new TokenHttpRequestMessage(requestModel, AccessToken)
            {
                Method     = HttpMethod.Post,
                RequestUri = new Uri(string.Concat(PushClient.BaseAddress, "/push/send"))
            };

            try
            {
                await PushClient.SendAsync(message);
            }
            catch (Exception e)
            {
                _logger.LogError(12334, e, "Unable to send push notification.");
            }
        }