Skip to content

Instantly share code, notes, and snippets.

@turtlepile
Created July 28, 2022 21:31
Show Gist options
  • Save turtlepile/79e3e894de741bfe243d559169d441be to your computer and use it in GitHub Desktop.
Save turtlepile/79e3e894de741bfe243d559169d441be to your computer and use it in GitHub Desktop.
FCM Notifications, notification class for server side
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
namespace Types.DTO
{
public class Notification
{
public string title { get; set; }
public string body { get; set; }
}
public class Message
{
/// <summary>
/// Gets or sets the notification information to be included in the message.
/// </summary>
[JsonProperty("notification")]
public Notification Notification { get; set; }
/// <summary>
/// Gets or sets the registration token of the device to which the message should be sent.
/// </summary>
[JsonProperty("token")]
public string Token { get; set; }
/// <summary>
/// Gets or sets the name of the FCM topic to which the message should be sent. Topic names
/// may contain the <c>/topics/</c> prefix.
/// </summary>
public string topic { get; set; }
/// <summary>
/// Gets or sets the Android-specific information to be included in the message.
/// </summary>
[JsonProperty("android")]
public AndroidConfig Android { get; set; }
}
public sealed class AndroidConfig
{
/// <summary>
/// Gets or sets a collapse key for the message. Collapse key serves as an identifier for a
/// group of messages that can be collapsed, so that only the last message gets sent when
/// delivery can be resumed. A maximum of 4 different collapse keys may be active at any
/// given time.
/// </summary>
[JsonProperty("collapse_key")]
public string CollapseKey { get; set; }
/// <summary>
/// Gets or sets the priority of the message.
/// </summary>
[JsonIgnore]
public Priority? Priority { get; set; }
/// <summary>
/// Gets or sets the time-to-live duration of the message.
/// </summary>
[JsonIgnore]
public TimeSpan? TimeToLive { get; set; }
/// <summary>
/// Gets or sets the package name of the application where the registration tokens must
/// match in order to receive the message.
/// </summary>
[JsonProperty("restricted_package_name")]
public string RestrictedPackageName { get; set; }
/// <summary>
/// Gets or sets a collection of key-value pairs that will be added to the message as data
/// fields. Keys and the values must not be null. When set, overrides any data fields set
/// on the top-level
/// <see cref="Message"/>.
/// </summary>
[JsonProperty("data")]
public IReadOnlyDictionary<string, string> Data { get; set; }
/// <summary>
/// Gets or sets the string representation of <see cref="Priority"/> as accepted by the FCM
/// backend service.
/// </summary>
[JsonProperty("priority")]
private string PriorityString
{
get
{
switch (this.Priority)
{
case DTO.Priority.High:
return "high";
case DTO.Priority.Normal:
return "normal";
default:
return null;
}
}
set
{
switch (value)
{
case "high":
this.Priority = DTO.Priority.High;
return;
case "normal":
this.Priority = DTO.Priority.High;
return;
default:
throw new ArgumentException(
$"Invalid priority value: {value}. Only 'high' and 'normal'"
+ " are allowed.");
}
}
}
/// <summary>
/// Gets or sets the string representation of <see cref="TimeToLive"/> as accepted by the
/// FCM backend service. The string ends in the suffix "s" (indicating seconds) and is
/// preceded by the number of seconds, with nanoseconds expressed as fractional seconds.
/// </summary>
[JsonProperty("ttl")]
private string TtlString
{
get
{
if (this.TimeToLive == null)
{
return null;
}
var totalSeconds = this.TimeToLive.Value.TotalSeconds;
var seconds = (long)Math.Floor(totalSeconds);
var subsecondNanos = (long)((totalSeconds - seconds) * 1e9);
if (subsecondNanos > 0)
{
return string.Format("{0}.{1:D9}s", seconds, subsecondNanos);
}
return string.Format("{0}s", seconds);
}
set
{
var segments = value.TrimEnd('s').Split('.');
var seconds = long.Parse(segments[0]);
var ttl = TimeSpan.FromSeconds(seconds);
if (segments.Length == 2)
{
var subsecondNanos = long.Parse(segments[1].TrimStart('0'));
ttl = ttl.Add(TimeSpan.FromMilliseconds(subsecondNanos / 1e6));
}
this.TimeToLive = ttl;
}
}
}
public class MessageMain
{
[JsonProperty("message")]
public Message message { get; set; }
}
/// <summary>
/// Priority levels that can be set on an <see cref="AndroidConfig"/>.
/// </summary>
public enum Priority
{
/// <summary>
/// High priority message.
/// </summary>
High,
/// <summary>
/// Normal priority message.
/// </summary>
Normal,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment