/// <summary> /// Create a notification /// </summary> /// <param name="osType"></param> /// <param name="args"></param> /// <returns></returns> public static INotificationService Make(int osType, params string[] args) { OsType os = (OsType)osType; INotificationService service; switch (os) { case OsType.Ios: if (args.Length < 2) { return(null); } service = new IOSNotificationService(args[0], args[1]); break; case OsType.Android: if (args.Length < 4) { return(null); } service = new AndroidNotificationService(args[2], args[3]); break; default: service = null; break; } return(service); }
public INotificationService GetNotificationService() { var existing = DependencyService.Get <INotificationService>(); if (existing == null) { if (Initializer.AppActivity == null) { throw new Exception("ctacke.Xamarin.Forms.Initializer.Init() has not been called."); } existing = new AndroidNotificationService(Initializer.AppActivity); } return(existing); }
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)); } }
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)); } }