gehgassi_backend/gehGassi.External/Services/PushNotificationService.cs

1141 lines
48 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection.Metadata;
using System.Text.Json;
using System.Threading.Tasks;
using gehGassi.Core.Interfaces;
using gehGassi.Domain.Common;
using gehGassi.Domain.Pushnotifications;
using gehGassi.Dto.Messages;
using gehGassi.External.Helper;
using Microsoft.Azure.NotificationHubs;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;
using NotificationPlatform = Microsoft.Azure.NotificationHubs.NotificationPlatform;
namespace gehGassi.External.Services
{
/// <summary>
/// Service der Push-Notifications senden kann
/// </summary>
public class PushNotificationService : IPushNotificationService
{
private readonly IDeviceService _deviceService;
private readonly IStringLocalizer<PushNotificationService> _localizer;
private readonly string _connectionString;
private readonly string _hubName;
private readonly NotificationHubClient _hubClient;
/// <summary>
/// Erstellt eine Instanz
/// </summary>
/// <param name="pushNotificationOptions">Instanz von PushNotificationOptions</param>
/// <param name="deviceService">Instanz eines IDeviceService</param>
/// <param name="localizer">Instanz eines IStringLocalizer</param>
public PushNotificationService(IOptions<PushNotificationOptions> pushNotificationOptions, IDeviceService deviceService, IStringLocalizer<PushNotificationService> localizer)
{
_deviceService = deviceService;
_localizer = localizer;
_connectionString = pushNotificationOptions.Value.ConnectionString;
_hubName = pushNotificationOptions.Value.HubName;
_hubClient = new NotificationHubClient(_connectionString, _hubName);
}
/// <summary>
/// Registrieren eines Gerätes für Pushnotifications
/// </summary>
/// <param name="deviceInstallation">Informationen für die Installation / Registrierung</param>
/// <returns>True wenn erfolgreich, false sonst</returns>
public async Task<bool> CreateOrUpdateInstallationAsync(DeviceInstallation deviceInstallation)
{
var installation = new Installation
{
InstallationId = deviceInstallation.InstallationId,
PushChannel = deviceInstallation.Channel,
Platform = (NotificationPlatform)deviceInstallation.NotificationPlatform,
Tags = deviceInstallation.Tags
};
try
{
await _hubClient.CreateOrUpdateInstallationAsync(installation);
}
catch(Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
return false;
}
return true;
}
/// <summary>
/// Löschen der Registrierung eines Gerätes für Pushnotifications
/// </summary>
/// <param name="installationId">Id der Installation</param>
/// <returns></returns>
public async Task<bool> DeleteInstallationByIdAsync(string installationId)
{
if (string.IsNullOrWhiteSpace(installationId))
return false;
try
{
await _hubClient.DeleteInstallationAsync(installationId);
}
catch
{
return false;
}
return true;
}
/// <summary>
/// Senden einer Pushnotification Nachricht
/// </summary>
/// <param name="notificationMessage">Nachricht die gesendet werden soll</param>
/// <returns>true wenn gesendet, false sonst</returns>
public async Task<bool> SendNotificationAsync(NotificationMessage notificationMessage)
{
var androidTemplate = PushnotificationTemplateGenerator.GetAndroid(notificationMessage.Title, notificationMessage.Message, notificationMessage.Silent, notificationMessage.Payload);
var androidTemplateV1 = PushnotificationTemplateGenerator.GetAndroidV1(notificationMessage.Title, notificationMessage.Message, notificationMessage.Silent, notificationMessage.Payload);
var iosTemplate = PushnotificationTemplateGenerator.GetIos(notificationMessage.Title, notificationMessage.Message, notificationMessage.Silent, notificationMessage.Payload);
try
{
if (notificationMessage.Tags.Count == 0)
{
await SendPlatformNotificationsAsync(androidTemplate, androidTemplateV1, iosTemplate);
}
else
{
await SendPlatformNotificationsAsync(androidTemplate, androidTemplateV1, iosTemplate, notificationMessage.Tags);
}
return true;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
return false;
}
/// <summary>
/// Senden einer Pushnotification Nachricht
/// </summary>
/// <param name="notificationMessage">Nachricht die gesendet werden soll</param>
/// <param name="tags">Liste zusätzlicher Tags für die Adressierung der Ziele</param>
/// <returns>true wenn gesendet, false sonst</returns>
public async Task<bool> SendNotificationAsync(NotificationMessage notificationMessage, List<string> tags)
{
var androidTemplate = PushnotificationTemplateGenerator.GetAndroid(notificationMessage.Title, notificationMessage.Message, notificationMessage.Silent, notificationMessage.Payload);
var androidTemplateV1 = PushnotificationTemplateGenerator.GetAndroidV1(notificationMessage.Title, notificationMessage.Message, notificationMessage.Silent, notificationMessage.Payload);
var iosTemplate = PushnotificationTemplateGenerator.GetIos(notificationMessage.Title, notificationMessage.Message, notificationMessage.Silent, notificationMessage.Payload);
if (tags is { Count: > 0 })
{
notificationMessage.Tags.AddRange(tags);
}
try
{
if (notificationMessage.Tags.Count == 0)
{
await SendPlatformNotificationsAsync(androidTemplate, androidTemplateV1, iosTemplate);
}
else
{
await SendPlatformNotificationsAsync(androidTemplate, androidTemplateV1, iosTemplate, notificationMessage.Tags);
}
return true;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
return false;
}
public async Task ListRegistrationsAsync()
{
var registrations = await _hubClient.GetAllRegistrationsAsync(0);
foreach (var registration in registrations)
{
System.Diagnostics.Debug.WriteLine($"Registration: {registration.RegistrationId} TAGS: {string.Join(" | ", registration.Tags)}");
}
}
/// <summary>
/// Sendet eine Pushnotification an einen App-User wenn eine neue Nachricht für ihn eingetroffen ist
/// </summary>
/// <param name="receiverId">ID des App-Users</param>
/// <param name="conversationId">Id des Konversation</param>
/// <returns>true wenn erfolgreich, false sonst</returns>
public async Task<bool> SendNewMessageAsync(string receiverId, string conversationId)
{
var devices = await _deviceService.GetAllAsync(receiverId);
if (devices.Any())
{
var device = devices.OrderByDescending(c => c.LastUpdate).First();
CultureInfo.CurrentCulture = new CultureInfo(device.Language);
var payload = GeneratePayload(conversationId, SystemMessageTables.Message, SystemMessageType.MessageReceived, (int)SystemMessageType.MessageReceived);
var pushMessage = new NotificationMessage
{
Tags = new List<string>() { $"appuserid:{receiverId}" },
Title = _localizer["Push_NewMessage_Title"],
Message = _localizer["Push_NewMessage_Message"],
Payload = payload
};
try
{
var success = await SendNotificationAsync(pushMessage);
return success;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
return false;
}
/// <summary>
/// Sendet eine Pushnotification an einen App-User wenn eine neue Text-Nachricht von gehgassi für ihn eingetroffen ist
/// </summary>
/// <param name="receiverId">ID des App-Users</param>
/// <param name="messageId">Id der Nachricht</param>
/// <returns>true wenn erfolgreich, false sonst</returns>
public async Task<bool> SendNewTextMessageAsync(string receiverId, string messageId)
{
var devices = await _deviceService.GetAllAsync(receiverId);
if (devices.Any())
{
var device = devices.OrderByDescending(c => c.LastUpdate).First();
CultureInfo.CurrentCulture = new CultureInfo(device.Language);
var payload = GeneratePayload(messageId, SystemMessageTables.Message, SystemMessageType.Text, (int)SystemMessageType.Text);
var pushMessage = new NotificationMessage
{
Tags = new List<string>() { $"appuserid:{receiverId}" },
Title = _localizer["Push_NewTextMessage_Title"],
Message = _localizer["Push_NewTextMessage_Message"],
Payload = payload
};
try
{
var success = await SendNotificationAsync(pushMessage);
return success;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
return false;
}
/// <summary>
/// Sendet eine Pushnotification an einen App-User wenn ein neuer Walk für ihn eingetroffen ist
/// </summary>
/// <param name="receiverId">ID des App-Users</param>
/// <param name="walkId">Id des Walks</param>
/// <returns>true wenn erfolgreich, false sonst</returns>
public async Task<bool> SendNewWalkAsync(string receiverId, string walkId)
{
var devices = await _deviceService.GetAllAsync(receiverId);
if (devices.Any())
{
var device = devices.OrderByDescending(c => c.LastUpdate).First();
CultureInfo.CurrentCulture = new CultureInfo(device.Language);
var payload = GeneratePayload(walkId, SystemMessageTables.Walk, SystemMessageType.WalkAdded, (int)SystemMessageType.WalkAdded, AppMode.DogWalker);
var pushMessage = new NotificationMessage
{
Tags = new List<string>() { $"appuserid:{receiverId}" },
Title = _localizer["Push_NewWalk_Title"],
Message = _localizer["Push_NewWalk_Message"],
Payload = payload
};
try
{
var success = await SendNotificationAsync(pushMessage);
return success;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
return false;
}
/// <summary>
/// Sendet eine Pushnotification an einen App-User wenn sich eine öffentliche Anfrage geändert hat
/// </summary>
/// <param name="receiverId">ID des App-Users</param>
/// <param name="publicWalkRequestId">Id des öffentlichen Anfrage</param>
/// <returns>true wenn erfolgreich, false sonst</returns>
public async Task<bool> SendPublicWalkRequestChangedAsync(string receiverId, string publicWalkRequestId)
{
var devices = await _deviceService.GetAllAsync(receiverId);
if (devices.Any())
{
var device = devices.OrderByDescending(c => c.LastUpdate).First();
CultureInfo.CurrentCulture = new CultureInfo(device.Language);
var payload = GeneratePayload(publicWalkRequestId, SystemMessageTables.PublicWalkRequest, SystemMessageType.PublicWalkRequestChanged, (int)SystemMessageType.PublicWalkRequestChanged, AppMode.DogWalker);
var pushMessage = new NotificationMessage
{
Tags = new List<string>() { $"appuserid:{receiverId}" },
Title = _localizer["Push_PublicWalkRequestChanged_Title"],
Message = _localizer["Push_PublicWalkRequestChanged_Message"],
Payload = payload
};
try
{
var success = await SendNotificationAsync(pushMessage);
return success;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
return false;
}
/// <summary>
/// Sendet eine Pushnotification an einen App-User wenn sich eine öffentliche Anfrage in der Nähe verfügbar ist
/// </summary>
/// <param name="receiverId">ID des App-Users</param>
/// <param name="publicWalkRequestId">Id des öffentlichen Anfrage</param>
/// <returns>true wenn erfolgreich, false sonst</returns>
public async Task<bool> SendPublicWalkRequestAvailableAsync(string receiverId, string publicWalkRequestId)
{
var devices = await _deviceService.GetAllAsync(receiverId);
if (devices.Any())
{
var device = devices.OrderByDescending(c => c.LastUpdate).First();
CultureInfo.CurrentCulture = new CultureInfo(device.Language);
var payload = GeneratePayload(publicWalkRequestId, SystemMessageTables.PublicWalkRequest, SystemMessageType.PublicWalkRequestChanged, (int)SystemMessageType.PublicWalkRequestChanged, AppMode.DogWalker);
var pushMessage = new NotificationMessage
{
Tags = new List<string>() { $"appuserid:{receiverId}" },
Title = _localizer["Push_PublicWalkRequestAvailable_Title"],
Message = _localizer["Push_PublicWalkRequestAvailable_Message"],
Payload = payload
};
try
{
var success = await SendNotificationAsync(pushMessage);
return success;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
return false;
}
/// <summary>
/// Sendet eine Pushnotification an einen App-User wenn eine Antwort auf eine öffentliche Anfrage erstellt wurde
/// </summary>
/// <param name="receiverId">ID des App-Users</param>
/// <param name="publicWalkRequestId">Id des öffentlichen Anfrage</param>
/// <returns>true wenn erfolgreich, false sonst</returns>
public async Task<bool> SendPublicWalkResponseAddedAsync(string receiverId, string publicWalkRequestId)
{
var devices = await _deviceService.GetAllAsync(receiverId);
if (devices.Any())
{
var device = devices.OrderByDescending(c => c.LastUpdate).First();
CultureInfo.CurrentCulture = new CultureInfo(device.Language);
var payload = GeneratePayload(publicWalkRequestId, SystemMessageTables.PublicWalkRequest, SystemMessageType.PublicWalkResponseAdded, (int)SystemMessageType.PublicWalkResponseAdded, AppMode.DogOwner);
var pushMessage = new NotificationMessage
{
Tags = new List<string>() { $"appuserid:{receiverId}" },
Title = _localizer["Push_PublicWalkResponseAdded_Title"],
Message = _localizer["Push_PublicWalkResponseAdded_Message"],
Payload = payload
};
try
{
var success = await SendNotificationAsync(pushMessage);
return success;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
return false;
}
/// <summary>
/// Sendet eine Pushnotification an einen App-User wenn eine Antwort auf eine öffentliche Anfrage sich geändert hat
/// </summary>
/// <param name="receiverId">ID des App-Users</param>
/// <param name="publicWalkRequestId">Id des öffentlichen Anfrage</param>
/// <returns>true wenn erfolgreich, false sonst</returns>
public async Task<bool> SendPublicWalkResponseChangedAsync(string receiverId, string publicWalkRequestId)
{
var devices = await _deviceService.GetAllAsync(receiverId);
if (devices.Any())
{
var device = devices.OrderByDescending(c => c.LastUpdate).First();
CultureInfo.CurrentCulture = new CultureInfo(device.Language);
var payload = GeneratePayload(publicWalkRequestId, SystemMessageTables.PublicWalkRequest, SystemMessageType.PublicWakResponseChanged, (int)SystemMessageType.PublicWakResponseChanged, AppMode.DogOwner);
var pushMessage = new NotificationMessage
{
Tags = new List<string>() { $"appuserid:{receiverId}" },
Title = _localizer["Push_PublicWalkResponseChanged_Title"],
Message = _localizer["Push_PublicWalkResponseChanged_Message"],
Payload = payload
};
try
{
var success = await SendNotificationAsync(pushMessage);
return success;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
return false;
}
/// <summary>
/// Sendet eine Pushnotification an einen App-User wenn eine Antwort auf eine öffentliche Anfrage stonriert wurde
/// </summary>
/// <param name="receiverId">ID des App-Users</param>
/// <param name="publicWalkRequestId">Id des öffentlichen Anfrage</param>
/// <returns>true wenn erfolgreich, false sonst</returns>
public async Task<bool> SendPublicWalkResponseCancelledAsync(string receiverId, string publicWalkRequestId)
{
var devices = await _deviceService.GetAllAsync(receiverId);
if (devices.Any())
{
var device = devices.OrderByDescending(c => c.LastUpdate).First();
CultureInfo.CurrentCulture = new CultureInfo(device.Language);
var payload = GeneratePayload(publicWalkRequestId, SystemMessageTables.PublicWalkRequest, SystemMessageType.PublicWalkResponseCancelled, (int)SystemMessageType.PublicWalkResponseCancelled, AppMode.DogOwner);
var pushMessage = new NotificationMessage
{
Tags = new List<string>() { $"appuserid:{receiverId}" },
Title = _localizer["Push_PublicWalkResponseCancelled_Title"],
Message = _localizer["Push_PublicWalkResponseCancelled_Message"],
Payload = payload
};
try
{
var success = await SendNotificationAsync(pushMessage);
return success;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
return false;
}
/// <summary>
/// Sendet eine Pushnotification an einen App-User wenn eine Antwort auf eine öffentliche Anfrage abgelehnt wurde
/// </summary>
/// <param name="receiverId">ID des App-Users</param>
/// <param name="publicWalkRequestId">Id des öffentlichen Anfrage</param>
/// <returns>true wenn erfolgreich, false sonst</returns>
public async Task<bool> SendPublicWalkResponseDeclinedAsync(string receiverId, string publicWalkRequestId)
{
var devices = await _deviceService.GetAllAsync(receiverId);
if (devices.Any())
{
var device = devices.OrderByDescending(c => c.LastUpdate).First();
CultureInfo.CurrentCulture = new CultureInfo(device.Language);
var payload = GeneratePayload(publicWalkRequestId, SystemMessageTables.PublicWalkRequest, SystemMessageType.PublicWalkResponseDeclined, (int)SystemMessageType.PublicWalkResponseDeclined, AppMode.DogWalker);
var pushMessage = new NotificationMessage
{
Tags = new List<string>() { $"appuserid:{receiverId}" },
Title = _localizer["Push_PublicWalkResponseDeclined_Title"],
Message = _localizer["Push_PublicWalkResponseDeclined_Message"],
Payload = payload
};
try
{
var success = await SendNotificationAsync(pushMessage);
return success;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
return false;
}
public async Task<bool> SendPublicWalkRequestCancelledAsync(string receiverId, string publicWalkRequestId)
{
var devices = await _deviceService.GetAllAsync(receiverId);
if (devices.Any())
{
var device = devices.OrderByDescending(c => c.LastUpdate).First();
CultureInfo.CurrentCulture = new CultureInfo(device.Language);
var payload = GeneratePayload(publicWalkRequestId, SystemMessageTables.PublicWalkRequest, SystemMessageType.PublicWalkRequestCancelled, (int)SystemMessageType.PublicWalkRequestCancelled, AppMode.DogWalker);
var pushMessage = new NotificationMessage
{
Tags = new List<string>() { $"appuserid:{receiverId}" },
Title = _localizer["Push_PublicWalkRequestCancelled_Title"],
Message = _localizer["Push_PublicWalkRequestCancelled_Message"],
Payload = payload
};
try
{
var success = await SendNotificationAsync(pushMessage);
return success;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
return false;
}
/// <summary>
/// Sendet eine Pushnotification an einen App-User wenn ein Walk storniert wurde
/// </summary>
/// <param name="receiverId">ID des App-Users</param>
/// <param name="walkId">Id des Walks</param>
/// <param name="appMode">App-Mode</param>
/// <returns>true wenn erfolgreich, false sonst</returns>
public async Task<bool> SendWalkCancelledAsync(string receiverId, string walkId, AppMode appMode)
{
var devices = await _deviceService.GetAllAsync(receiverId);
if (devices.Any())
{
var device = devices.OrderByDescending(c => c.LastUpdate).First();
CultureInfo.CurrentCulture = new CultureInfo(device.Language);
var payload = GeneratePayload(walkId, SystemMessageTables.Walk, SystemMessageType.WalkCancelled, (int)SystemMessageType.WalkCancelled, appMode);
var pushMessage = new NotificationMessage
{
Tags = new List<string>() { $"appuserid:{receiverId}" },
Title = _localizer["Push_WalkCancelled_Title"],
Message = _localizer["Push_WalkCancelled_Message"],
Payload = payload
};
try
{
var success = await SendNotificationAsync(pushMessage);
return success;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
return false;
}
/// <summary>
/// Sendet eine Pushnotification an einen App-User wenn ein Walk gestartet wurde
/// </summary>
/// <param name="receiverId">ID des App-Users</param>
/// <param name="walkId">Id des Walks</param>
/// <returns>true wenn erfolgreich, false sonst</returns>
public async Task<bool> SendWalkStartedAsync(string receiverId, string walkId)
{
var devices = await _deviceService.GetAllAsync(receiverId);
if (devices.Any())
{
var device = devices.OrderByDescending(c => c.LastUpdate).First();
CultureInfo.CurrentCulture = new CultureInfo(device.Language);
var payload = GeneratePayload(walkId, SystemMessageTables.Walk, SystemMessageType.WalkStarted, (int)SystemMessageType.WalkStarted, AppMode.DogOwner);
var pushMessage = new NotificationMessage
{
Tags = new List<string>() { $"appuserid:{receiverId}" },
Title = _localizer["Push_WalkStarted_Title"],
Message = _localizer["Push_WalkStarted_Message"],
Payload = payload
};
try
{
var success = await SendNotificationAsync(pushMessage);
return success;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
return false;
}
/// <summary>
/// Sendet eine Pushnotification an einen App-User wenn ein Walk abgeschlossen wurde
/// </summary>
/// <param name="receiverId">ID des App-Users</param>
/// <param name="walkId">Id des Walks</param>
/// <returns>true wenn erfolgreich, false sonst</returns>
public async Task<bool> SendWalkCompletedAsync(string receiverId, string walkId)
{
var devices = await _deviceService.GetAllAsync(receiverId);
if (devices.Any())
{
var device = devices.OrderByDescending(c => c.LastUpdate).First();
CultureInfo.CurrentCulture = new CultureInfo(device.Language);
var payload = GeneratePayload(walkId, SystemMessageTables.Walk, SystemMessageType.WalkCompleted, (int)SystemMessageType.WalkCompleted, AppMode.DogOwner);
var pushMessage = new NotificationMessage
{
Tags = new List<string>() { $"appuserid:{receiverId}" },
Title = _localizer["Push_WalkCompleted_Title"],
Message = _localizer["Push_WalkCompleted_Message"],
Payload = payload
};
try
{
var success = await SendNotificationAsync(pushMessage);
return success;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
return false;
}
/// <summary>
/// Sendet eine Pushnotification an einen App-User wenn ein Walk abgeschlossen wurde als Erinnerung
/// </summary>
/// <param name="receiverId">ID des App-Users</param>
/// <param name="walkId">Id des Walks</param>
/// <returns>true wenn erfolgreich, false sonst</returns>
public async Task<bool> SendWalkCompletedReminderAsync(string receiverId, string walkId)
{
var devices = await _deviceService.GetAllAsync(receiverId);
if (devices.Any())
{
var device = devices.OrderByDescending(c => c.LastUpdate).First();
CultureInfo.CurrentCulture = new CultureInfo(device.Language);
var payload = GeneratePayload(walkId, SystemMessageTables.Walk, SystemMessageType.WalkCompleted, (int)SystemMessageType.WalkCompleted, AppMode.DogOwner);
var pushMessage = new NotificationMessage
{
Tags = new List<string>() { $"appuserid:{receiverId}" },
Title = _localizer["Push_WalkCompleted_Reminder_Title"],
Message = _localizer["Push_WalkCompleted_Reminder_Message"],
Payload = payload
};
try
{
var success = await SendNotificationAsync(pushMessage);
return success;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
return false;
}
/// <summary>
/// Sendet eine Pushnotification an einen App-User wenn ein Walk angenommen wurde
/// </summary>
/// <param name="receiverId">ID des App-Users</param>
/// <param name="walkId">Id des Walks</param>
/// <returns>true wenn erfolgreich, false sonst</returns>
public async Task<bool> SendWalkAcceptedAsync(string receiverId, string walkId)
{
var devices = await _deviceService.GetAllAsync(receiverId);
if (devices.Any())
{
var device = devices.OrderByDescending(c => c.LastUpdate).First();
CultureInfo.CurrentCulture = new CultureInfo(device.Language);
var payload = GeneratePayload(walkId, SystemMessageTables.Walk, SystemMessageType.WalkAccepted, (int)SystemMessageType.WalkAccepted, AppMode.DogOwner);
var pushMessage = new NotificationMessage
{
Tags = new List<string>() { $"appuserid:{receiverId}" },
Title = _localizer["Push_WalkAccepted_Title"],
Message = _localizer["Push_WalkAccepted_Message"],
Payload = payload
};
try
{
var success = await SendNotificationAsync(pushMessage);
return success;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
return false;
}
/// <summary>
/// Sendet eine Pushnotification an einen App-User wenn ein Walk abgelehnt wurde
/// </summary>
/// <param name="receiverId">ID des App-Users</param>
/// <param name="walkId">Id des Walks</param>
/// <returns>true wenn erfolgreich, false sonst</returns>
public async Task<bool> SendWalkDeclinedAsync(string receiverId, string walkId)
{
var devices = await _deviceService.GetAllAsync(receiverId);
if (devices.Any())
{
var device = devices.OrderByDescending(c => c.LastUpdate).First();
CultureInfo.CurrentCulture = new CultureInfo(device.Language);
var payload = GeneratePayload(walkId, SystemMessageTables.Walk, SystemMessageType.WalkDeclined, (int)SystemMessageType.WalkDeclined, AppMode.DogOwner);
var pushMessage = new NotificationMessage
{
Tags = new List<string>() { $"appuserid:{receiverId}" },
Title = _localizer["Push_WalkDeclined_Title"],
Message = _localizer["Push_WalkDeclined_Message"],
Payload = payload
};
try
{
var success = await SendNotificationAsync(pushMessage);
return success;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
return false;
}
/// <summary>
/// Sendet eine Pushnotification an einen App-User wenn ein Walk angefragt wurde
/// </summary>
/// <param name="receiverId">ID des App-Users</param>
/// <param name="walkId">Id des Walks</param>
/// <returns>true wenn erfolgreich, false sonst</returns>
public async Task<bool> SendWalkRequestedAsync(string receiverId, string walkId)
{
var devices = await _deviceService.GetAllAsync(receiverId);
if (devices.Any())
{
var device = devices.OrderByDescending(c => c.LastUpdate).First();
CultureInfo.CurrentCulture = new CultureInfo(device.Language);
var payload = GeneratePayload(walkId, SystemMessageTables.Walk, SystemMessageType.WalkRequested, (int)SystemMessageType.WalkRequested, AppMode.DogWalker);
var pushMessage = new NotificationMessage
{
Tags = new List<string>() { $"appuserid:{receiverId}" },
Title = _localizer["Push_WalkRequested_Title"],
Message = _localizer["Push_WalkRequested_Message"],
Payload = payload
};
try
{
var success = await SendNotificationAsync(pushMessage);
return success;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
return false;
}
/// <summary>
/// Sendet eine Pushnotification an einen App-User wenn ein Walk bestätigt wurde
/// </summary>
/// <param name="receiverId">ID des App-Users</param>
/// <param name="walkId">Id des Walks</param>
/// <returns>true wenn erfolgreich, false sonst</returns>
public async Task<bool> SendWalkConfirmedAsync(string receiverId, string walkId)
{
var devices = await _deviceService.GetAllAsync(receiverId);
if (devices.Any())
{
var device = devices.OrderByDescending(c => c.LastUpdate).First();
CultureInfo.CurrentCulture = new CultureInfo(device.Language);
var payload = GeneratePayload(walkId, SystemMessageTables.Walk, SystemMessageType.WalkConfirmed, (int)SystemMessageType.WalkConfirmed, AppMode.DogWalker);
var pushMessage = new NotificationMessage
{
Tags = new List<string>() { $"appuserid:{receiverId}" },
Title = _localizer["Push_WalkConfirmed_Title"],
Message = _localizer["Push_WalkConfirmed_Message"],
Payload = payload
};
try
{
var success = await SendNotificationAsync(pushMessage);
return success;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
return false;
}
/// <summary>
/// Sendet eine Pushnotification an einen App-User wenn ein Walk reklamiert wurde
/// </summary>
/// <param name="receiverId">ID des App-Users</param>
/// <param name="walkId">Id des Walks</param>
/// <returns>true wenn erfolgreich, false sonst</returns>
public async Task<bool> SendWalkComplainedAsync(string receiverId, string walkId)
{
var devices = await _deviceService.GetAllAsync(receiverId);
if (devices.Any())
{
var device = devices.OrderByDescending(c => c.LastUpdate).First();
CultureInfo.CurrentCulture = new CultureInfo(device.Language);
var payload = GeneratePayload(walkId, SystemMessageTables.Walk, SystemMessageType.WalkComplained, (int)SystemMessageType.WalkComplained, AppMode.DogWalker);
var pushMessage = new NotificationMessage
{
Tags = new List<string>() { $"appuserid:{receiverId}" },
Title = _localizer["Push_WalkComplained_Title"],
Message = _localizer["Push_WalkComplained_Message"],
Payload = payload
};
try
{
var success = await SendNotificationAsync(pushMessage);
return success;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
return false;
}
/// <summary>
/// Sendet eine Pushnotification an einen App-User wenn ein Walk reklamiert wurde
/// </summary>
/// <param name="receiverId">ID des App-Users</param>
/// <param name="walkId">Id des Walks</param>
/// <param name="appMode">App-Mode</param>
/// <returns>true wenn erfolgreich, false sonst</returns>
public async Task<bool> SendWalkComplainedUpdateAsync(string receiverId, string walkId, AppMode appMode)
{
var devices = await _deviceService.GetAllAsync(receiverId);
if (devices.Any())
{
var device = devices.OrderByDescending(c => c.LastUpdate).First();
CultureInfo.CurrentCulture = new CultureInfo(device.Language);
var payload = GeneratePayload(walkId, SystemMessageTables.Walk, SystemMessageType.WalkComplainedUpdate, (int)SystemMessageType.WalkComplainedUpdate, appMode);
var pushMessage = new NotificationMessage
{
Tags = new List<string>() { $"appuserid:{receiverId}" },
Title = _localizer["Push_WalkComplainedUpdate_Title"],
Message = _localizer["Push_WalkComplainedUpdate_Message"],
Payload = payload
};
try
{
var success = await SendNotificationAsync(pushMessage);
return success;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
return false;
}
/// <summary>
/// Sendet eine Pushnotification an einen Hundebesitzer wenn die Bezahlung für einen ein Walk gescheitert ist.
/// Wird verwendet wenn das Payment nicht funktioniert hat und der Hundebesitzer den Walk neu buchen muss.
/// </summary>
/// <param name="receiverId">ID des App-Users</param>
/// <param name="walkId">Id des Walks</param>
/// <returns>true wenn erfolgreich, false sonst</returns>
public async Task<bool> SendWalkPaymentFailedAsync(string receiverId, string walkId)
{
var devices = await _deviceService.GetAllAsync(receiverId);
if (devices.Any())
{
var device = devices.OrderByDescending(c => c.LastUpdate).First();
CultureInfo.CurrentCulture = new CultureInfo(device.Language);
var payload = GeneratePayload(walkId, SystemMessageTables.Walk, SystemMessageType.WalkChanged, (int)SystemMessageType.WalkChanged, AppMode.DogOwner);
var pushMessage = new NotificationMessage
{
Tags = new List<string>() { $"appuserid:{receiverId}" },
Title = _localizer["Push_WalkPaymentFailed_Title"],
Message = _localizer["Push_WalkPaymentFailed_Message"],
Payload = payload
};
try
{
var success = await SendNotificationAsync(pushMessage);
return success;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
return false;
}
/// <summary>
/// Sendet eine Pushnotification an einen App-User wenn ein KYC-Status (Dokument) geändert wurde
/// </summary>
/// <param name="receiverId">ID des App-Users</param>
/// <param name="documentId">Id des KYC-Dokuments</param>
/// <returns>true wenn erfolgreich, false sonst</returns>
public async Task<bool> SendIdentityDocumentUpdateAsync(string receiverId, string documentId)
{
var devices = await _deviceService.GetAllAsync(receiverId);
if (devices.Any())
{
var device = devices.OrderByDescending(c => c.LastUpdate).First();
CultureInfo.CurrentCulture = new CultureInfo(device.Language);
var payload = GeneratePayload(documentId, SystemMessageTables.IdentityDocument, SystemMessageType.IdentityDocumentUpdate, (int)SystemMessageType.IdentityDocumentUpdate);
var pushMessage = new NotificationMessage
{
Tags = new List<string>() { $"appuserid:{receiverId}" },
Title = _localizer["Push_IdentityDocumentUpdate_Title"],
Message = _localizer["Push_IdentityDocumentUpdate_Message"],
Payload = payload
};
try
{
var success = await SendNotificationAsync(pushMessage);
return success;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
return false;
}
/// <summary>
/// Sendet eine Pushnotification an einen App-User wenn ein Auszahlungsstatus geändert wurde
/// </summary>
/// <param name="receiverId">ID des App-Users</param>
/// <param name="payoutId">Id der Auszahlung</param>
/// <returns>true wenn erfolgreich, false sonst</returns>
public async Task<bool> SendPayoutUpdateAsync(string receiverId, string payoutId)
{
var devices = await _deviceService.GetAllAsync(receiverId);
if (devices.Any())
{
var device = devices.OrderByDescending(c => c.LastUpdate).First();
CultureInfo.CurrentCulture = new CultureInfo(device.Language);
var payload = GeneratePayload(payoutId, SystemMessageTables.Payout, SystemMessageType.PayoutUpdate, (int)SystemMessageType.PayoutUpdate);
var pushMessage = new NotificationMessage
{
Tags = new List<string>() { $"appuserid:{receiverId}" },
Title = _localizer["Push_PayoutUpdate_Title"],
Message = _localizer["Push_PayoutUpdate_Message"],
Payload = payload
};
try
{
var success = await SendNotificationAsync(pushMessage);
return success;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
return false;
}
/// <summary>
/// Sendet eine Pushnotification an einen App-User dass ein Logout durchgeführt werden soll
/// </summary>
/// <param name="receiverId">ID des App-Users</param>
/// <returns>true wenn erfolgreich, false sonst</returns>
public async Task<bool> SendLogoutAsync(string receiverId)
{
var devices = await _deviceService.GetAllAsync(receiverId);
if (devices.Any())
{
var device = devices.OrderByDescending(c => c.LastUpdate).First();
CultureInfo.CurrentCulture = new CultureInfo(device.Language);
var payload = GeneratePayload(string.Empty, SystemMessageTables.System, SystemMessageType.Logout, (int)SystemMessageType.Logout);
var pushMessage = new NotificationMessage
{
Tags = new List<string>() { $"appuserid:{receiverId}" },
Title = string.Empty,
Message = string.Empty,
Payload = payload,
Silent = true
};
try
{
var success = await SendNotificationAsync(pushMessage);
return success;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
return false;
}
#region private
/// <summary>
/// Senden einer Nachricht via Platform-Send-Funktion
/// </summary>
/// <param name="androidPayload">Android-Nachricht</param>
/// <param name="androidPayloadV1">Android Nachricht V1 Version</param>
/// <param name="iOSPayload">Ios-Nachricht</param>
/// <returns>Task</returns>
private Task SendPlatformNotificationsAsync(string androidPayload, string androidPayloadV1, string iOSPayload)
{
var sendTasks = new List<Task>()
{
_hubClient.SendFcmV1NativeNotificationAsync(androidPayloadV1),
_hubClient.SendAppleNativeNotificationAsync(iOSPayload)
};
if(DateTime.UtcNow < new DateTime(2024, 07, 1, 0,0,0,DateTimeKind.Utc))
sendTasks.Add(_hubClient.SendFcmNativeNotificationAsync(androidPayload));
return Task.WhenAll(sendTasks);
}
/// <summary>
/// Senden einer Nachricht via Platform-Send-Funktion
/// </summary>
/// <param name="androidPayload">Android-Nachricht</param>
/// <param name="androidPayloadV1">Android Nachricht V1 Version</param>
/// <param name="iOSPayload">Ios-Nachricht</param>
/// <param name="tags">Adressierungs-Tags</param>
/// <returns>Task</returns>
private Task SendPlatformNotificationsAsync(string androidPayload, string androidPayloadV1, string iOSPayload, IEnumerable<string> tags)
{
var tagList = tags as string[] ?? tags.ToArray();
var sendTasks = new List<Task>()
{
_hubClient.SendFcmV1NativeNotificationAsync(androidPayloadV1, tagList),
_hubClient.SendAppleNativeNotificationAsync(iOSPayload, tagList)
};
if (DateTime.UtcNow < new DateTime(2024, 07, 1, 0, 0, 0, DateTimeKind.Utc))
sendTasks.Add(_hubClient.SendFcmNativeNotificationAsync(androidPayload, tagList));
return Task.WhenAll(sendTasks);
}
/// <summary>
/// Erzeugt den Payload für eine Push-Nachricht
/// </summary>
/// <param name="key">Id des verknüpften Objektes</param>
/// <param name="table">Typ des Objektes das verbunden wurde</param>
/// <param name="type">Typ der Systemnachricht</param>
/// <param name="notificationId">Id der Nachricht für Cancel</param>
/// <param name="appMode">Optinal: Typ des Appusers damit man bei jenen die beides sind auch unterscheiden kann</param>
/// <param name="data">Optional: Daten der Nachricht</param>
/// <param name="title">Optional: Text-Title der Nachricht</param>
/// <param name="body">Optional: Text-Inhalt der Nachricht</param>
/// <returns></returns>
private string GeneratePayload(string key, string table, SystemMessageType type, int notificationId, AppMode? appMode = null, string data = null, string title = null, string body = null)
{
var payload = new ServerPushNotificationInfo
{
Key = key,
Table = table,
Type = type,
NotificationId = notificationId,
AppMode = appMode,
Data = data,
Title = title,
Body = body
};
var jsonPayload = JsonSerializer.Serialize(payload);
return jsonPayload;
}
#endregion
}
}