예제 #1
0
        public async Task <IActionResult> Send(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = "v1/Notifications")]
            [RequestBodyType(typeof(SendNotification), "Enviar push notification")] HttpRequest request,
            ILogger logger, Microsoft.Azure.WebJobs.ExecutionContext context)
        {
            try
            {
                var form = await request.GetJsonBody <SendNotification, SendNotificationValidator>();

                if (!form.IsValid)
                {
                    logger.LogInformation($"Invalid form data.");
                    return(form.ToBadRequest());
                }

                var input = form.Value;

                AndroidTokenRepository    androidTokenRepository    = new AndroidTokenRepository();
                IOSTokenRepository        iOSTokenRepository        = new IOSTokenRepository();
                IOSNotificationRepository iOSNotificationRepository = new IOSNotificationRepository();
                NotificationRepository    notificationRepository    = new NotificationRepository();

                AndroidNotificationService androidNotificationService = new AndroidNotificationService();

                var androidTokens = await androidTokenRepository.GetNotificationByUsers(input.UserIds, input.CompanyId, input.ApplicationId);

                var iOSTokens = await iOSTokenRepository.GetNotificationByUsers(input.UserIds, input.CompanyId);

                if (androidTokens.Count() < 1 && iOSTokens.Count() < 1)
                {
                    return(new BadRequestObjectResult("Nenhum usuário cadastrado."));
                }

                Notification notification = new Notification
                {
                    ApplicationId = input.ApplicationId,
                    Title         = input.Title,
                    Message       = input.Message
                };

                notification.Id = await notificationRepository.CreateNotification(notification);

                var responseList = await androidNotificationService.Send(androidTokens, notification, input.Data, logger);

                //Adicionar código quando tiver algo do IOS
                //List<IOSNotification> responseListIOS = new List<IOSNotification>();

                //foreach (var iOSToken in iOSTokens)
                //{
                //    IOSNotification iOSNotification = new IOSNotification
                //    {
                //        TokenId = iOSToken.Id,
                //        NotificationId = notification.Id
                //    };
                //    try
                //    {
                //        await SendPushIOSAsync(iOSNotification, notification, iOSToken, input.Data, context);

                //    }
                //    finally
                //    {
                //        responseListIOS.Add(iOSNotification);
                //        try
                //        {
                //            await iOSNotificationRepository.CreateNotification(iOSNotification);
                //        }
                //        catch (Exception ex)
                //        {
                //            logger.LogError(ex.Message);
                //        }
                //    }
                //}

                return(new OkObjectResult(responseList));
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return(new BadRequestObjectResult(ex.Message));
            }
        }
예제 #2
0
        public async Task <IActionResult> Send(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = "Notifications")]
            [RequestBodyType(typeof(SendNotification), "Enviar push notification")] HttpRequest request,
            ILogger logger, Microsoft.Azure.WebJobs.ExecutionContext context)
        {
            try
            {
                string requestBody = await new StreamReader(request.Body).ReadToEndAsync();
                var    input       = JsonConvert.DeserializeObject <SendNotification>(requestBody);
                input.ApplicationId = 2;

                var validator        = new SendNotificationValidator();
                var validationResult = validator.Validate(input);

                if (!validationResult.IsValid)
                {
                    return(new BadRequestObjectResult(validationResult.Errors.Select(e => new {
                        Field = e.PropertyName,
                        Error = e.ErrorMessage
                    })));
                }

                AndroidTokenRepository     androidTokenRepository     = new AndroidTokenRepository();
                IOSTokenRepository         iOSTokenRepository         = new IOSTokenRepository();
                IOSNotificationRepository  iOSNotificationRepository  = new IOSNotificationRepository();
                NotificationRepository     notificationRepository     = new NotificationRepository();
                AndroidNotificationService androidNotificationService = new AndroidNotificationService();

                var androidTokens = await androidTokenRepository.GetNotificationByUsers(input.UserIds, input.CompanyId, input.ApplicationId);

                var iOSTokens = await iOSTokenRepository.GetNotificationByUsers(input.UserIds, input.CompanyId);

                if (androidTokens.Count() < 1 && iOSTokens.Count() < 1)
                {
                    return(new BadRequestObjectResult("Nenhum usuário cadastrado."));
                }

                Notification notification = new Notification
                {
                    ApplicationId = input.ApplicationId,
                    Title         = input.Title,
                    Message       = input.Message
                };

                notification.Id = await notificationRepository.CreateNotification(notification);

                var responseList = await androidNotificationService.Send(androidTokens, notification, input.Data, logger);

                //Adicionar código quando tiver algo do IOS
                //List<IOSNotification> responseListIOS = new List<IOSNotification>();

                //foreach (var iOSToken in iOSTokens)
                //{
                //    IOSNotification iOSNotification = new IOSNotification
                //    {
                //        TokenId = iOSToken.Id,
                //        NotificationId = notification.Id
                //    };
                //    try
                //    {
                //        await SendPushIOSAsync(iOSNotification, notification, iOSToken, input.Data, context);

                //    }
                //    finally
                //    {
                //        responseListIOS.Add(iOSNotification);
                //        try
                //        {
                //            await iOSNotificationRepository.CreateNotification(iOSNotification);
                //        }
                //        catch (Exception ex)
                //        {
                //            logger.LogError(ex.Message);
                //        }
                //    }
                //}

                return(new OkObjectResult(responseList));
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return(new BadRequestObjectResult(ex.Message));
            }
        }