From b7c0c075348532dd019579353794be89f41c0bce Mon Sep 17 00:00:00 2001 From: Jamie Date: Mon, 13 Aug 2018 00:49:02 +0100 Subject: [PATCH] Added rich notifications for mobile --- src/Ombi.Api.Notifications/IOneSignalApi.cs | 2 +- .../Models/OneSignalNotificationBody.cs | 14 +++++++---- src/Ombi.Api.Notifications/OneSignalApi.cs | 13 +++++++++- .../Agents/MobileNotification.cs | 24 +++++++++---------- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/Ombi.Api.Notifications/IOneSignalApi.cs b/src/Ombi.Api.Notifications/IOneSignalApi.cs index 2e3ef106e..6de64d11e 100644 --- a/src/Ombi.Api.Notifications/IOneSignalApi.cs +++ b/src/Ombi.Api.Notifications/IOneSignalApi.cs @@ -6,6 +6,6 @@ namespace Ombi.Api.Notifications { public interface IOneSignalApi { - Task PushNotification(List playerIds, string message); + Task PushNotification(List playerIds, string message, bool isAdminNotification, int requestId, int requestType); } } \ No newline at end of file diff --git a/src/Ombi.Api.Notifications/Models/OneSignalNotificationBody.cs b/src/Ombi.Api.Notifications/Models/OneSignalNotificationBody.cs index 6c024fa67..e65222bd5 100644 --- a/src/Ombi.Api.Notifications/Models/OneSignalNotificationBody.cs +++ b/src/Ombi.Api.Notifications/Models/OneSignalNotificationBody.cs @@ -4,18 +4,22 @@ { public string app_id { get; set; } public string[] include_player_ids { get; set; } - public Data data { get; set; } + public object data { get; set; } + public Button[] buttons { get; set; } public Contents contents { get; set; } } - public class Data - { - public string foo { get; set; } - } public class Contents { public string en { get; set; } } + public class Button + { + public string id { get; set; } + public string text { get; set; } + //public string icon { get; set; } + } + } \ No newline at end of file diff --git a/src/Ombi.Api.Notifications/OneSignalApi.cs b/src/Ombi.Api.Notifications/OneSignalApi.cs index d4760bb09..8d5fc04a9 100644 --- a/src/Ombi.Api.Notifications/OneSignalApi.cs +++ b/src/Ombi.Api.Notifications/OneSignalApi.cs @@ -20,7 +20,7 @@ namespace Ombi.Api.Notifications private readonly IApplicationConfigRepository _appConfig; private const string ApiUrl = "https://onesignal.com/api/v1/notifications"; - public async Task PushNotification(List playerIds, string message) + public async Task PushNotification(List playerIds, string message, bool isAdminNotification, int requestId, int requestType) { if (!playerIds.Any()) { @@ -39,6 +39,17 @@ namespace Ombi.Api.Notifications include_player_ids = playerIds.ToArray() }; + if (isAdminNotification) + { + // Add the action buttons + body.data = new { requestid = requestId, requestType = requestType}; + body.buttons = new[] + { + new Button {id = "approve", text = "Approve Request"}, + new Button {id = "deny", text = "Deny Request"}, + }; + } + request.AddJsonBody(body); var result = await _api.Request(request); diff --git a/src/Ombi.Notifications/Agents/MobileNotification.cs b/src/Ombi.Notifications/Agents/MobileNotification.cs index bfb7dcd88..8559c043d 100644 --- a/src/Ombi.Notifications/Agents/MobileNotification.cs +++ b/src/Ombi.Notifications/Agents/MobileNotification.cs @@ -57,7 +57,7 @@ namespace Ombi.Notifications.Agents // Get admin devices var playerIds = await GetAdmins(NotificationType.NewRequest); - await Send(playerIds, notification, settings); + await Send(playerIds, notification, settings, model, true); } protected override async Task NewIssue(NotificationOptions model, MobileNotificationSettings settings) @@ -75,7 +75,7 @@ namespace Ombi.Notifications.Agents // Get admin devices var playerIds = await GetAdmins(NotificationType.Issue); - await Send(playerIds, notification, settings); + await Send(playerIds, notification, settings, model); } protected override async Task IssueComment(NotificationOptions model, MobileNotificationSettings settings) @@ -97,13 +97,13 @@ namespace Ombi.Notifications.Agents { // Send to user var playerIds = GetUsers(model, NotificationType.IssueComment); - await Send(playerIds, notification, settings); + await Send(playerIds, notification, settings, model); } else { // Send to admin var playerIds = await GetAdmins(NotificationType.IssueComment); - await Send(playerIds, notification, settings); + await Send(playerIds, notification, settings, model); } } } @@ -124,7 +124,7 @@ namespace Ombi.Notifications.Agents // Send to user var playerIds = GetUsers(model, NotificationType.IssueResolved); - await Send(playerIds, notification, settings); + await Send(playerIds, notification, settings, model); } @@ -149,7 +149,7 @@ namespace Ombi.Notifications.Agents }; // Get admin devices var playerIds = await GetAdmins(NotificationType.Test); - await Send(playerIds, notification, settings); + await Send(playerIds, notification, settings, model); } protected override async Task RequestDeclined(NotificationOptions model, MobileNotificationSettings settings) @@ -168,7 +168,7 @@ namespace Ombi.Notifications.Agents // Send to user var playerIds = GetUsers(model, NotificationType.RequestDeclined); await AddSubscribedUsers(playerIds); - await Send(playerIds, notification, settings); + await Send(playerIds, notification, settings, model); } protected override async Task RequestApproved(NotificationOptions model, MobileNotificationSettings settings) @@ -188,7 +188,7 @@ namespace Ombi.Notifications.Agents var playerIds = GetUsers(model, NotificationType.RequestApproved); await AddSubscribedUsers(playerIds); - await Send(playerIds, notification, settings); + await Send(playerIds, notification, settings, model); } protected override async Task AvailableRequest(NotificationOptions model, MobileNotificationSettings settings) @@ -207,20 +207,20 @@ namespace Ombi.Notifications.Agents var playerIds = GetUsers(model, NotificationType.RequestAvailable); await AddSubscribedUsers(playerIds); - await Send(playerIds, notification, settings); + await Send(playerIds, notification, settings, model); } protected override Task Send(NotificationMessage model, MobileNotificationSettings settings) { throw new NotImplementedException(); } - protected async Task Send(List playerIds, NotificationMessage model, MobileNotificationSettings settings) + protected async Task Send(List playerIds, NotificationMessage model, MobileNotificationSettings settings, NotificationOptions requestModel, bool isAdminNotification = false) { if (playerIds == null || !playerIds.Any()) { return; } - var response = await _api.PushNotification(playerIds, model.Message); + var response = await _api.PushNotification(playerIds, model.Message, isAdminNotification, requestModel.RequestId, (int)requestModel.RequestType); _logger.LogDebug("Sent message to {0} recipients with message id {1}", response.recipients, response.id); } @@ -239,7 +239,7 @@ namespace Ombi.Notifications.Agents } var playerIds = user.NotificationUserIds.Select(x => x.PlayerId).ToList(); - await Send(playerIds, notification, settings); + await Send(playerIds, notification, settings, model); } private async Task> GetAdmins(NotificationType type)