This commit is contained in:
Jamie 2018-02-01 16:31:49 +00:00
parent b0d229975b
commit c370909619
13 changed files with 338 additions and 3 deletions

View file

@ -0,0 +1,11 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Ombi.Api.Notifications.Models;
namespace Ombi.Api.Notifications
{
public interface IOneSignalApi
{
Task<OneSignalNotificationResponse> PushNotification(List<string> playerIds, string message);
}
}

View file

@ -0,0 +1,21 @@
namespace Ombi.Api.Notifications.Models
{
public class OneSignalNotificationBody
{
public string app_id { get; set; }
public string[] include_player_ids { get; set; }
public Data data { get; set; }
public Contents contents { get; set; }
}
public class Data
{
public string foo { get; set; }
}
public class Contents
{
public string en { get; set; }
}
}

View file

@ -0,0 +1,9 @@
namespace Ombi.Api.Notifications.Models
{
public class OneSignalNotificationResponse
{
public string id { get; set; }
public int recipients { get; set; }
}
}

View file

@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Ombi.Api\Ombi.Api.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,48 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Ombi.Api.Notifications.Models;
using Ombi.Store.Entities;
using Ombi.Store.Repository;
namespace Ombi.Api.Notifications
{
public class OneSignalApi : IOneSignalApi
{
public OneSignalApi(IApi api, IApplicationConfigRepository repo)
{
_api = api;
_appConfig = repo;
}
private readonly IApi _api;
private readonly IApplicationConfigRepository _appConfig;
private const string ApiUrl = "https://onesignal.com/api/v1/notifications";
public async Task<OneSignalNotificationResponse> PushNotification(List<string> playerIds, string message)
{
if (!playerIds.Any())
{
return null;
}
var id = await _appConfig.Get(ConfigurationTypes.Notification);
var request = new Request(string.Empty, ApiUrl, HttpMethod.Post);
var body = new OneSignalNotificationBody
{
app_id = id.Value,
contents = new Contents
{
en = message
},
include_player_ids = playerIds.ToArray()
};
request.AddJsonBody(body);
var result = await _api.Request<OneSignalNotificationResponse>(request);
return result;
}
}
}