113 lines
5.0 KiB
C#
113 lines
5.0 KiB
C#
using Microsoft.Azure.NotificationHubs;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace gehGassi.External.Helper
|
|
{
|
|
/// <summary>
|
|
/// Vorlage für Pushnotification Nachrichten
|
|
/// </summary>
|
|
public class PushnotificationTemplateGenerator
|
|
{
|
|
public const string Android_Alert = "{ \"notification\": { \"title\" : \"$(title)\", \"body\" : \"$(message)\"} }";
|
|
public const string Android_Alert_Payload = "{ \"notification\": { \"title\" : \"$(title)\", \"body\" : \"$(message)\"}, \"data\": $(payload) }";
|
|
public const string Android_Silent_Payload = "{\"data\": $(payload) }";
|
|
|
|
//Änderung auf Version V1
|
|
public const string Android_Alert_v1 = "{\"message\": { \"notification\": { \"title\" : \"$(title)\", \"body\" : \"$(message)\"} } }";
|
|
public const string Android_Alert_Payload_v1 = "{\"message\": { \"notification\": { \"title\" : \"$(title)\", \"body\" : \"$(message)\"}, \"data\": $(payload) } }";
|
|
public const string Android_Silent_Payload_v1 = "{\"message\": {\"data\": $(payload) } }";
|
|
|
|
public const string Ios_Alert = "{ \"aps\" : {\"alert\" : {\"title\": \"$(title)\", \"body\": \"$(message)\"}} }";
|
|
public const string Ios_Alert_Payload = "{ \"aps\" : {\"alert\" : {\"title\": \"$(title)\", \"body\": \"$(message)\"}}, $(payload) }";
|
|
public const string Iso_Silent_Payload = "{ \"aps\" : {\"content-available\" : 1, \"apns-priority\": 5, \"sound\" : \"\", \"badge\" : 0}, $(payload) }";
|
|
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Notification-Template für Android
|
|
/// </summary>
|
|
/// <param name="title">Title</param>
|
|
/// <param name="message">Nachricht</param>
|
|
/// <param name="silent">true wenn eine Silent-Message gesendet werden soll, false sonst</param>
|
|
/// <param name="payload">Optional: Payload der mitgesendet werden soll</param>
|
|
/// <returns>Erstelltes Message-Template</returns>
|
|
public static string GetAndroid(string title, string message, bool silent, object payload = null)
|
|
{
|
|
var payloadJson = string.Empty;
|
|
if (payload != null)
|
|
{
|
|
payloadJson = payload is string ? payload.ToString() : JsonSerializer.Serialize(payload);
|
|
}
|
|
|
|
return silent switch
|
|
{
|
|
false when payload == null => Android_Alert.Replace("$(title)", title).Replace("$(message)", message),
|
|
false => Android_Alert_Payload.Replace("$(title)", title).Replace("$(message)", message).Replace("$(payload)", payloadJson),
|
|
_ => Android_Silent_Payload.Replace("$(payload)", payloadJson)
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Notification-Template für Android V1
|
|
/// </summary>
|
|
/// <param name="title">Title</param>
|
|
/// <param name="message">Nachricht</param>
|
|
/// <param name="silent">true wenn eine Silent-Message gesendet werden soll, false sonst</param>
|
|
/// <param name="payload">Optional: Payload der mitgesendet werden soll</param>
|
|
/// <returns>Erstelltes Message-Template</returns>
|
|
public static string GetAndroidV1(string title, string message, bool silent, object payload = null)
|
|
{
|
|
var payloadJson = string.Empty;
|
|
if (payload != null)
|
|
{
|
|
payloadJson = payload is string ? payload.ToString() : JsonSerializer.Serialize(payload);
|
|
}
|
|
|
|
return silent switch
|
|
{
|
|
false when payload == null => Android_Alert_v1.Replace("$(title)", title).Replace("$(message)", message),
|
|
false => Android_Alert_Payload_v1.Replace("$(title)", title).Replace("$(message)", message).Replace("$(payload)", payloadJson),
|
|
_ => Android_Silent_Payload_v1.Replace("$(payload)", payloadJson)
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Notification-Template für Ios
|
|
/// </summary>
|
|
/// <param name="title">Title</param>
|
|
/// <param name="message">Nachricht</param>
|
|
/// <param name="silent">true wenn eine Silent-Message gesendet werden soll, false sonst</param>
|
|
/// <param name="payload">Optional: Payload der mitgesendet werden soll</param>
|
|
/// <returns>Erstelltes Message-Template</returns>
|
|
public static string GetIos(string title, string message, bool silent, object payload = null)
|
|
{
|
|
var payloadJson = string.Empty;
|
|
if (payload != null)
|
|
{
|
|
payloadJson = payload is string ? payload.ToString() : JsonSerializer.Serialize(payload);
|
|
if (payloadJson.StartsWith("{") && payloadJson.EndsWith("}"))
|
|
{
|
|
payloadJson = payloadJson.TrimStart('{');
|
|
payloadJson = payloadJson.TrimEnd('}');
|
|
}
|
|
|
|
}
|
|
|
|
return silent switch
|
|
{
|
|
false when payload == null => Ios_Alert.Replace("$(title)", title).Replace("$(message)", message),
|
|
false => Ios_Alert_Payload.Replace("$(title)", title).Replace("$(message)", message).Replace("$(payload)", payloadJson),
|
|
_ => Iso_Silent_Payload.Replace("$(payload)", payloadJson)
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
}
|