Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Core/Resgrid.Config/ChatConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ public static class ChatConfig
public static string NovuEnvironmentId = "";
public static string NovuApplicationId = "";
public static string NovuSecretKey = "";

public static string NovuUnitFcmProviderId = "";
public static string NovuResponderFcmProviderId = "";
}
}
3 changes: 3 additions & 0 deletions Core/Resgrid.Model/Providers/Models/INovuProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ public interface INovuProvider
{
Task<bool> CreateUserSubscriber(string userId, string code, int departmentId, string email, string firstName, string lastName);
Task<bool> CreateUnitSubscriber(int unitId, string code, int departmentId, string unitName);
Task<bool> UpdateUserSubscriberFcm(string userId, string code, string token);
Task<bool> UpdateUnitSubscriberFcm(int unitId, string code, string token);

}
14 changes: 10 additions & 4 deletions Core/Resgrid.Services/PushService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ public class PushService : IPushService
private readonly INotificationProvider _notificationProvider;
private readonly IUnitNotificationProvider _unitNotificationProvider;
private readonly IUserProfileService _userProfileService;
private readonly INovuProvider _novuProvider;

public PushService(IPushLogsService pushLogsService, INotificationProvider notificationProvider,
IUserProfileService userProfileService, IUnitNotificationProvider unitNotificationProvider)
IUserProfileService userProfileService, IUnitNotificationProvider unitNotificationProvider,
INovuProvider novuProvider)
{
_pushLogsService = pushLogsService;
_notificationProvider = notificationProvider;
_userProfileService = userProfileService;
_unitNotificationProvider = unitNotificationProvider;
_novuProvider = novuProvider;
}

public async Task<bool> Register(PushUri pushUri)
Expand All @@ -37,7 +40,7 @@ public async Task<bool> Register(PushUri pushUri)
// We just store the full Device Id in the PushUri object, the hashed version is for Azure
//var existingPushUri = _pushUriService.GetPushUriByPlatformDeviceId((Platforms)pushUri.PlatformType, pushUri.DeviceId);
List<PushRegistrationDescription> usersDevices = null;

try
{
usersDevices = await _notificationProvider.GetRegistrationsByUserId(pushUri.UserId);
Expand Down Expand Up @@ -78,6 +81,9 @@ public async Task<bool> RegisterUnit(PushUri pushUri)
catch (TimeoutException)
{ }

if (pushUri.UnitId.HasValue)
await _novuProvider.UpdateUnitSubscriberFcm(pushUri.UnitId.Value, pushUri.PushLocation, pushUri.DeviceId);


if (usersDevices == null || !usersDevices.Any(x => x.Tags.Contains(string.Format("unitId:{0}", pushUri.UnitId.ToString()))))
await _unitNotificationProvider.RegisterPush(pushUri);
Expand Down Expand Up @@ -161,7 +167,7 @@ public async Task<bool> PushCall(StandardPushCall call, string userId, UserProfi

if (profile != null && profile.SendPush)
await _notificationProvider.SendAllNotifications(call.SubTitle, call.Title, userId, string.Format("C{0}", call.CallId), ConvertCallPriorityToSound((int)call.Priority, priority), true, call.ActiveCallCount, color);

return true;
}

Expand Down Expand Up @@ -236,7 +242,7 @@ private byte[] ReadResource(string fileName)
// var exception = (PushSharp.WindowsPhone.WindowsPhoneNotificationSendFailureException) notificationFailureException;
// _pushLogsService.LogPushResult(exception.MessageStatus.DeviceConnectionStatus.ToString(),
// exception.MessageStatus.HttpStatus.ToString(), exception.MessageStatus.MessageID.ToString(),
// exception.MessageStatus.NotificationStatus.ToString(), exception.MessageStatus.SubscriptionStatus.ToString(),
// exception.MessageStatus.NotificationStatus.ToString(), exception.MessageStatus.SubscriptionStatus.ToString(),
// exception.MessageStatus.Notification.EndPointUrl, exception);

// //Console.WriteLine("Failure: " + notification.Platform.ToString() + " -> " + notificationFailureException.Message + " -> " + notification.ToString());
Expand Down
53 changes: 52 additions & 1 deletion Providers/Resgrid.Providers.Messaging/NovuProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using Novu;
using System.Text;
using Newtonsoft.Json;
using Novu;
using Novu.Domain.Models.Subscribers;
using Resgrid.Config;
using Resgrid.Framework;
using Resgrid.Model.Providers;
using static Resgrid.Framework.Testing.TestData;
Expand Down Expand Up @@ -55,5 +58,53 @@ public async Task<bool> CreateUnitSubscriber(int unitId, string code, int depart
{
return await CreateSubscriber($"{code}_Unit_{unitId}", departmentId, $"{code}_Unit_{unitId}@units.resgrid.net", unitName, "");
}

private async Task<bool> UpdateSubscriberFcm(string id, string token, string fcmId)
{
try
{
using (HttpClient client = new HttpClient())
{
var url = $"${ChatConfig.NovuBackendUrl}v1/subscribers/{id}/credentials";
var request = new HttpRequestMessage(HttpMethod.Put, url);
request.Headers.Add("Accept", "application/json");
request.Headers.Add("Authorization", $"ApiKey {ChatConfig.NovuSecretKey}");

var payload = new
{
providerId = "fcm",
credentials = new
{
deviceTokens = new string[] { token }
},
integrationIdentifier = fcmId
};
string jsonContent = JsonConvert.SerializeObject(payload);

request.Content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.SendAsync(request);

if (response.IsSuccessStatusCode)
return true;
else
return false;
}
}
catch (Exception e)
{
Logging.LogException(e, "Failed to add fcm token to novu subscriber");
return false;
}
}

public async Task<bool> UpdateUserSubscriberFcm(string userId, string code, string token)
{
return await UpdateSubscriberFcm($"{code}_User_{userId}", token, ChatConfig.NovuResponderFcmProviderId);
}

public async Task<bool> UpdateUnitSubscriberFcm(int unitId, string code, string token)
{
return await UpdateSubscriberFcm($"{code}_Unit_{unitId}", token, ChatConfig.NovuUnitFcmProviderId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,18 @@ public async Task<ActionResult<PushRegistrationResult>> RegisterUnitDevice([From
if (registrationInput == null)
return BadRequest();



PushRegisterionEvent pushRegisterionEvent = new PushRegisterionEvent();
pushRegisterionEvent.PushUriId = 0;
pushRegisterionEvent.UserId = UserId;
pushRegisterionEvent.PlatformType = registrationInput.Platform;
pushRegisterionEvent.PushLocation = "";

if (!string.IsNullOrWhiteSpace(registrationInput.Prefix))
pushRegisterionEvent.PushLocation = registrationInput.Prefix;
else
pushRegisterionEvent.PushLocation = "";

pushRegisterionEvent.DepartmentId = DepartmentId;
pushRegisterionEvent.DeviceId = registrationInput.Token;
pushRegisterionEvent.Uuid = registrationInput.DeviceUuid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@ public class PushRegistrationInput
/// Device UDID
/// </summary>
public string DeviceUuid { get; set; }

/// <summary>
/// Prefix
/// </summary>
public string Prefix { get; set; }
}
}
Loading
Loading