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
{
///
/// Service der Push-Notifications senden kann
///
public class PushNotificationService : IPushNotificationService
{
private readonly IDeviceService _deviceService;
private readonly IStringLocalizer _localizer;
private readonly string _connectionString;
private readonly string _hubName;
private readonly NotificationHubClient _hubClient;
///
/// Erstellt eine Instanz
///
/// Instanz von PushNotificationOptions
/// Instanz eines IDeviceService
/// Instanz eines IStringLocalizer
public PushNotificationService(IOptions pushNotificationOptions, IDeviceService deviceService, IStringLocalizer localizer)
{
_deviceService = deviceService;
_localizer = localizer;
_connectionString = pushNotificationOptions.Value.ConnectionString;
_hubName = pushNotificationOptions.Value.HubName;
_hubClient = new NotificationHubClient(_connectionString, _hubName);
}
///
/// Registrieren eines Gerätes für Pushnotifications
///
/// Informationen für die Installation / Registrierung
/// True wenn erfolgreich, false sonst
public async Task 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;
}
///
/// Löschen der Registrierung eines Gerätes für Pushnotifications
///
/// Id der Installation
///
public async Task DeleteInstallationByIdAsync(string installationId)
{
if (string.IsNullOrWhiteSpace(installationId))
return false;
try
{
await _hubClient.DeleteInstallationAsync(installationId);
}
catch
{
return false;
}
return true;
}
///
/// Senden einer Pushnotification Nachricht
///
/// Nachricht die gesendet werden soll
/// true wenn gesendet, false sonst
public async Task 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;
}
///
/// Senden einer Pushnotification Nachricht
///
/// Nachricht die gesendet werden soll
/// Liste zusätzlicher Tags für die Adressierung der Ziele
/// true wenn gesendet, false sonst
public async Task SendNotificationAsync(NotificationMessage notificationMessage, List 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)}");
}
}
///
/// Sendet eine Pushnotification an einen App-User wenn eine neue Nachricht für ihn eingetroffen ist
///
/// ID des App-Users
/// Id des Konversation
/// true wenn erfolgreich, false sonst
public async Task 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() { $"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;
}
///
/// Sendet eine Pushnotification an einen App-User wenn eine neue Text-Nachricht von gehgassi für ihn eingetroffen ist
///
/// ID des App-Users
/// Id der Nachricht
/// true wenn erfolgreich, false sonst
public async Task 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() { $"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;
}
///
/// Sendet eine Pushnotification an einen App-User wenn ein neuer Walk für ihn eingetroffen ist
///
/// ID des App-Users
/// Id des Walks
/// true wenn erfolgreich, false sonst
public async Task 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() { $"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;
}
///
/// Sendet eine Pushnotification an einen App-User wenn sich eine öffentliche Anfrage geändert hat
///
/// ID des App-Users
/// Id des öffentlichen Anfrage
/// true wenn erfolgreich, false sonst
public async Task 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() { $"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;
}
///
/// Sendet eine Pushnotification an einen App-User wenn sich eine öffentliche Anfrage in der Nähe verfügbar ist
///
/// ID des App-Users
/// Id des öffentlichen Anfrage
/// true wenn erfolgreich, false sonst
public async Task 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() { $"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;
}
///
/// Sendet eine Pushnotification an einen App-User wenn eine Antwort auf eine öffentliche Anfrage erstellt wurde
///
/// ID des App-Users
/// Id des öffentlichen Anfrage
/// true wenn erfolgreich, false sonst
public async Task 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() { $"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;
}
///
/// Sendet eine Pushnotification an einen App-User wenn eine Antwort auf eine öffentliche Anfrage sich geändert hat
///
/// ID des App-Users
/// Id des öffentlichen Anfrage
/// true wenn erfolgreich, false sonst
public async Task 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() { $"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;
}
///
/// Sendet eine Pushnotification an einen App-User wenn eine Antwort auf eine öffentliche Anfrage stonriert wurde
///
/// ID des App-Users
/// Id des öffentlichen Anfrage
/// true wenn erfolgreich, false sonst
public async Task 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() { $"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;
}
///
/// Sendet eine Pushnotification an einen App-User wenn eine Antwort auf eine öffentliche Anfrage abgelehnt wurde
///
/// ID des App-Users
/// Id des öffentlichen Anfrage
/// true wenn erfolgreich, false sonst
public async Task 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() { $"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 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() { $"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;
}
///
/// Sendet eine Pushnotification an einen App-User wenn ein Walk storniert wurde
///
/// ID des App-Users
/// Id des Walks
/// App-Mode
/// true wenn erfolgreich, false sonst
public async Task 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() { $"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;
}
///
/// Sendet eine Pushnotification an einen App-User wenn ein Walk gestartet wurde
///
/// ID des App-Users
/// Id des Walks
/// true wenn erfolgreich, false sonst
public async Task 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() { $"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;
}
///
/// Sendet eine Pushnotification an einen App-User wenn ein Walk abgeschlossen wurde
///
/// ID des App-Users
/// Id des Walks
/// true wenn erfolgreich, false sonst
public async Task 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() { $"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;
}
///
/// Sendet eine Pushnotification an einen App-User wenn ein Walk abgeschlossen wurde als Erinnerung
///
/// ID des App-Users
/// Id des Walks
/// true wenn erfolgreich, false sonst
public async Task 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() { $"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;
}
///
/// Sendet eine Pushnotification an einen App-User wenn ein Walk angenommen wurde
///
/// ID des App-Users
/// Id des Walks
/// true wenn erfolgreich, false sonst
public async Task 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() { $"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;
}
///
/// Sendet eine Pushnotification an einen App-User wenn ein Walk abgelehnt wurde
///
/// ID des App-Users
/// Id des Walks
/// true wenn erfolgreich, false sonst
public async Task 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() { $"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;
}
///
/// Sendet eine Pushnotification an einen App-User wenn ein Walk angefragt wurde
///
/// ID des App-Users
/// Id des Walks
/// true wenn erfolgreich, false sonst
public async Task 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() { $"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;
}
///
/// Sendet eine Pushnotification an einen App-User wenn ein Walk bestätigt wurde
///
/// ID des App-Users
/// Id des Walks
/// true wenn erfolgreich, false sonst
public async Task 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() { $"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;
}
///
/// Sendet eine Pushnotification an einen App-User wenn ein Walk reklamiert wurde
///
/// ID des App-Users
/// Id des Walks
/// true wenn erfolgreich, false sonst
public async Task 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() { $"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;
}
///
/// Sendet eine Pushnotification an einen App-User wenn ein Walk reklamiert wurde
///
/// ID des App-Users
/// Id des Walks
/// App-Mode
/// true wenn erfolgreich, false sonst
public async Task 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() { $"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;
}
///
/// 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.
///
/// ID des App-Users
/// Id des Walks
/// true wenn erfolgreich, false sonst
public async Task 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() { $"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;
}
///
/// Sendet eine Pushnotification an einen App-User wenn ein KYC-Status (Dokument) geändert wurde
///
/// ID des App-Users
/// Id des KYC-Dokuments
/// true wenn erfolgreich, false sonst
public async Task 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() { $"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;
}
///
/// Sendet eine Pushnotification an einen App-User wenn ein Auszahlungsstatus geändert wurde
///
/// ID des App-Users
/// Id der Auszahlung
/// true wenn erfolgreich, false sonst
public async Task 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() { $"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;
}
///
/// Sendet eine Pushnotification an einen App-User dass ein Logout durchgeführt werden soll
///
/// ID des App-Users
/// true wenn erfolgreich, false sonst
public async Task 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() { $"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
///
/// Senden einer Nachricht via Platform-Send-Funktion
///
/// Android-Nachricht
/// Android Nachricht V1 Version
/// Ios-Nachricht
/// Task
private Task SendPlatformNotificationsAsync(string androidPayload, string androidPayloadV1, string iOSPayload)
{
var sendTasks = new List()
{
_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);
}
///
/// Senden einer Nachricht via Platform-Send-Funktion
///
/// Android-Nachricht
/// Android Nachricht V1 Version
/// Ios-Nachricht
/// Adressierungs-Tags
/// Task
private Task SendPlatformNotificationsAsync(string androidPayload, string androidPayloadV1, string iOSPayload, IEnumerable tags)
{
var tagList = tags as string[] ?? tags.ToArray();
var sendTasks = new List()
{
_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);
}
///
/// Erzeugt den Payload für eine Push-Nachricht
///
/// Id des verknüpften Objektes
/// Typ des Objektes das verbunden wurde
/// Typ der Systemnachricht
/// Id der Nachricht für Cancel
/// Optinal: Typ des Appusers damit man bei jenen die beides sind auch unterscheiden kann
/// Optional: Daten der Nachricht
/// Optional: Text-Title der Nachricht
/// Optional: Text-Inhalt der Nachricht
///
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
}
}