public async Task <string> Send(NotificationJsonObj input) { try { await _pushNotificationHelper.SendNotification(input); return("success"); } catch (Exception ex) { return(ex.Message); } }
public async Task SendNotification(NotificationJsonObj model) { string webRootPath = _hostingEnvironment.WebRootPath; string contentRootPath = _hostingEnvironment.ContentRootPath; try { await _notificaionRepository.SendNotification(model); } catch (Exception ex) { //just throw throw ex; } }
public virtual void sendNotification() { var userList = new List <long>(); userList.Add(2); var notification = new NotificationJsonObj { Message = "Test Message", priority = "high", Title = "Test Title", to = "", notification = new NotificaionBodyDto { UserIdList = userList, body = "Test message", content_available = true, title = "Test Title" } }; _pushNotificationHelper.SendNotification(notification); }
public virtual async Task SendNotification(NotificationJsonObj input) { try { var appKey = "AIzaSyA0CLo0JhmgpxTvphpUKDDIhaSTkGThtdY";// _configuration["PushNotification:AppKey"]; var fcmHost = "https://fcm.googleapis.com/fcm/send"; var allUsers = input.notification.UserIdList; foreach (var item in allUsers) { //save notification log now var logModel = new PushNotificationLog { UserId = item, IsRead = false, NotificationSentOn = DateTime.Now, NotificationContent = input.notification.body, NotificationBody = JsonConvert.SerializeObject(input) }; await _notificationLogRepository.InsertAsync(logModel); } var devices = await _deviceRepository.GetAll().Where(x => !x.IsDeleted && allUsers.Contains(x.UserID)).ToListAsync(); foreach (var device in devices) { var notificationId = string.Empty; var notification = await _notificationLogRepository.GetAll().OrderByDescending(x => x.CreationTime).FirstOrDefaultAsync(x => x.UserId == device.UserID); if (notification != null) { notificationId = notification.Id.ToString(); } using (var client = new WebClient()) { // Set the header so it knows we are sending JSON. client.Headers[HttpRequestHeader.ContentType] = "application/json"; client.Headers[HttpRequestHeader.Authorization] = "key=" + appKey; var jsonobj = new NotificationJsonObj(); jsonobj.to = device.RegID.ToString(); jsonobj.priority = "high"; if (device.PlatformType.ToString().Trim().ToLower() == "ios" || device.PlatformType.ToString().Trim().ToLower() == "web") { jsonobj.priority = "high"; var noti = new NotificaionBodyDto(); //noti.priority = "high"; noti.title = input.Title; noti.body = input.Message; noti.code = "PUSH_NOTIFICATION"; //notification.content_available = true; jsonobj.notification = noti; } var data = new NotificaionBodyDto(); data.title = input.Title; data.body = input.Message; data.ReferenceId = string.IsNullOrEmpty(input.notification.ReferenceId) ? "" : input.notification.ReferenceId.ToString(); data.NotificationLogId = string.IsNullOrEmpty(notificationId)? Guid.Empty : new Guid(notificationId); data.code = "PUSH_NOTIFICATION"; jsonobj.data = data; //for react like javascript framework -- send in the body jsonobj.data.custom_notification = new { title = input.Title, body = input.Message, sound = "default", priority = "high", show_in_foreground = true, targetScreen = "notification_detail", notificationId = string.IsNullOrEmpty(notificationId) ? Guid.Empty : new Guid(notificationId) }; //sent notification try { var body = JsonConvert.SerializeObject(jsonobj); var response = client.UploadString(fcmHost, body); } catch (Exception) { //try to sent notification for all users continue; } } } } catch (Exception ex) { throw ex; } }