public async Task <IActionResult> Post([FromBody] NotificationInput input) { bool sent = false; var user = HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); if (input.DeviceTokens.Any()) { //Object creation var messageInformation = new Message() { notification = new Notification() { title = input.Title, text = input.Body }, data = input.Data, registration_ids = input.DeviceTokens }; //Object to JSON STRUCTURE => using Newtonsoft.Json; string jsonMessage = JsonConvert.SerializeObject(messageInformation); /* * ------ JSON STRUCTURE ------ * { * notification: { * title: "", * text: "" * }, * data: { * action: "Play", * playerId: 5 * }, * registration_ids = ["id1", "id2"] * } * ------ JSON STRUCTURE ------ */ //Create request to Firebase API var request = new HttpRequestMessage(HttpMethod.Post, FireBasePushNotificationsURL); request.Headers.TryAddWithoutValidation("Authorization", "key=" + ServerKey); request.Content = new StringContent(jsonMessage, Encoding.UTF8, "application/json"); HttpResponseMessage result; using (var client = new HttpClient()) { result = await client.SendAsync(request); sent = sent && result.IsSuccessStatusCode; } } return(Ok()); }
public async Task SendNotification([FromBody] NotificationInput input) { var userWhichAltered = await _userService.GetUserById(JwtReader.GetUserId()); var documentTitle = ""; var documentToNotifyAbout = await _editorDocumentRepository.FirstOrDefaultAsync(d => d.Id == input.DocumentId); if (documentToNotifyAbout != null) { input.UserId = documentToNotifyAbout.UserId; documentTitle = documentToNotifyAbout.Title; } var linkId = ""; var message = ""; switch (input.NotificationType) { case NotificationTypeEnum.Follow: message = userWhichAltered.FirstName + " está te seguindo"; linkId = "/user/profile/" + input.UserId; break; case NotificationTypeEnum.Like: message = userWhichAltered.FirstName + " curtiu o seu componente do documento \"" + documentTitle + "\""; linkId = "/document/" + input.DocumentId; break; case NotificationTypeEnum.Dislike: message = userWhichAltered.FirstName + " deu dislike em seu componente do documento \"" + documentTitle + "\""; linkId = "/document/" + input.DocumentId; break; case NotificationTypeEnum.Comment: message = userWhichAltered.FirstName + " comentou em seu componente do documento \"" + documentTitle + "\""; linkId = "/document/" + input.DocumentId; break; case NotificationTypeEnum.Love: break; default: throw new ArgumentOutOfRangeException(); } await Repository.AddAsync(new Notification { UserId = input.UserId, WasRead = false, Message = message, NotificationType = input.NotificationType, LinkId = linkId }); await Context.SaveChangesAsync(); }
public async Task <object> EditNotification([FromBody] NotificationInput notificationInput) { notificationInput.FromUserId = HttpContext.User.GetUserId(); var result = new ApiResult(); if (!notificationInput.NotificationId.HasValue) { result.Code = 400; result.Message = "编辑通知失败:无通知ID"; return(result); } if (!await _notificationService.SaveAsync(notificationInput, ModelState)) { result.Code = 400; result.Message = "编辑通知失败:" + ModelState.FirstErrorMessage(); return(result); } result.Code = 200; result.Message = "编辑通知成功"; return(result); }
public async Task <bool> SaveAsync(NotificationInput notificationInput, ModelStateDictionary modelState) { var result = await _notificationManager.SaveAsync(notificationInput, modelState); if (result && !notificationInput.NotificationId.HasValue) { var apiResultNotification = new ApiResultNotification { Code = 201, Title = notificationInput.Title, Message = notificationInput.Message, }; if (notificationInput.ToUserId.HasValue) { var client = _hubContext.Clients.User(notificationInput.ToUserId.Value.ToString()); client.ReceiveMessage(apiResultNotification).ContinueWithOnFaultedHandleLog(_logger); } else { _hubContext.Clients.All.ReceiveMessage(apiResultNotification).ContinueWithOnFaultedHandleLog(_logger); } } return(result); }
public async Task <bool> SaveAsync(NotificationInput notificationInput, ModelStateDictionary modelState) { var result = await _notificationRepository.SaveAsync(notificationInput, modelState); if (result && !notificationInput.NotificationId.HasValue) { var apiResultNotification = new ApiResultNotification { Code = 201, Title = notificationInput.Title, Message = notificationInput.Message, }; if (notificationInput.ToUserId.HasValue) { var client = _hubContext.Clients.User(notificationInput.ToUserId.Value.ToString()); await client.ReceiveMessage(apiResultNotification); } else { await _hubContext.Clients.All.ReceiveMessage(apiResultNotification); } } return(result); }
public NotificationResults SendEmail(NotificationInput input) { CreditsHero.Messaging.Dtos.NotificationResults results = new CreditsHero.Messaging.Dtos.NotificationResults(); return((CreditsHero.Messaging.Dtos.NotificationResults)_creditsHeroConnect.CallCreditsHeroService <CreditsHero.Messaging.Dtos.NotificationResults>(results, input, "api/services/app/Notification/SendEmailNotification")); }
public IHttpActionResult SendNotification(NotificationInput notification) { Hub.Clients.Group("GroupName").BroadcastCustomerGreeting("notification"); return(Ok()); }