using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using gehGassiApp.Core.Interfaces;
using gehGassiApp.Core.Mapper;
using gehGassiApp.Domain.Common;
using gehGassiApp.Domain.Pushnotifications;
using Platform = gehGassiApp.Domain.Common.Platform;
namespace gehGassiApp.Core.Services
{
///
/// Service der alle Funktionen rund um Pushnotifications bereitstellt
///
public class PushnotificationService : IPushnotificationService
{
private readonly ICommunicationService _communicationService;
///
/// Erstellt eine Instanz
///
/// Instanz eines ICommunicationService
public PushnotificationService(ICommunicationService communicationService)
{
_communicationService = communicationService;
}
///
/// Registrieren / Aktualisieren eines Devices für Push-Notifications
///
/// Eindeutige ID
/// Push-Channel / Hub
/// Sprache des Gerätes
/// Aktuelles Accesstoken
/// CancellationToken
/// True wenn Registrierung erfolgreich, false sonst
public async Task RegisterAsycn(string installationId, string channel, string language, string accessToken, CancellationToken token)
{
var isConnected = await _communicationService.IsConnected();
if (isConnected)
{
var deviceInstallation = new DeviceInstallation
{
InstallationId = installationId,
Channel = channel,
Platform = Platform.Android,
NotificationPlatform = NotificationPlatform.FcmV1,
Language = language
};
if (DeviceInfo.Platform == DevicePlatform.iOS)
{
deviceInstallation.Platform = Platform.Ios;
deviceInstallation.NotificationPlatform = NotificationPlatform.Apns;
}
var dto = deviceInstallation.ToDto();
var successResult = await _communicationService.RegisterPushnotificationsAsync(dto, accessToken, token);
return successResult.Value;
}
return false;
}
///
/// Deregistrieren eines Devices für Push-Notifications
///
/// Eindeutige ID
/// Aktuelles Accesstoken
/// CancellationToken
/// True wenn Deregistrierung erfolgreich, false sonst
public async Task UnregisterAsycn(string installationId, string accessToken, CancellationToken token)
{
var isConnected = await _communicationService.IsConnected();
if (isConnected)
{
var successResult = await _communicationService.UnregisterPushnotificationsAsync(installationId, accessToken, token);
return successResult.Value;
}
return false;
}
}
}