Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fcm): Added support for specifying analytics label in notifications #89

Merged
merged 6 commits into from Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@ bin/
obj/
.vscode/
.vs/
.idea/
71 changes: 71 additions & 0 deletions FirebaseAdmin/FirebaseAdmin.Tests/Messaging/MessageTest.cs
Expand Up @@ -75,6 +75,10 @@ public void Notification()
Title = "title",
Body = "body",
},
FcmOptions = new FcmOptions()
RannyRanny marked this conversation as resolved.
Show resolved Hide resolved
{
AnalyticsLabel = "label",
},
};
var expected = new JObject()
{
Expand All @@ -86,6 +90,12 @@ public void Notification()
{ "body", "body" },
}
},
{
"fcm_options", new JObject()
{
{ "analytics_label", "label" },
}
},
};
this.AssertJsonEquals(expected, message);
}
Expand Down Expand Up @@ -117,6 +127,10 @@ public void MessageDeserialization()
{
Data = new Dictionary<string, string>() { { "key", "value" } },
},
FcmOptions = new FcmOptions()
{
AnalyticsLabel = "label",
},
};
var json = NewtonsoftJsonSerializer.Instance.Serialize(original);
var copy = NewtonsoftJsonSerializer.Instance.Deserialize<Message>(json);
Expand All @@ -128,6 +142,7 @@ public void MessageDeserialization()
original.Android.RestrictedPackageName, copy.Android.RestrictedPackageName);
Assert.Equal(original.Apns.Aps.AlertString, copy.Apns.Aps.AlertString);
Assert.Equal(original.Webpush.Data, copy.Webpush.Data);
Assert.Equal(original.FcmOptions.AnalyticsLabel, copy.FcmOptions.AnalyticsLabel);
}

[Fact]
Expand Down Expand Up @@ -236,6 +251,10 @@ public void AndroidConfig()
BodyLocArgs = new List<string>() { "arg3", "arg4" },
ChannelId = "channel-id",
},
FcmOptions = new AndroidFcmOptions()
{
AnalyticsLabel = "label",
},
},
};
var expected = new JObject()
Expand Down Expand Up @@ -266,6 +285,12 @@ public void AndroidConfig()
{ "channel_id", "channel-id" },
}
},
{
"fcm_options", new JObject()
{
{ "analytics_label", "label" },
}
},
}
},
};
Expand Down Expand Up @@ -843,6 +868,10 @@ public void ApnsConfig()
{ "custom-key3", "custom-data" },
{ "custom-key4", true },
},
FcmOptions = new ApnsFcmOptions()
{
AnalyticsLabel = "label",
},
},
};
var expected = new JObject()
Expand Down Expand Up @@ -879,6 +908,12 @@ public void ApnsConfig()
{ "custom-key4", true },
}
},
{
"fcm_options", new JObject()
{
{ "analytics_label", "label" },
}
},
}
},
};
Expand Down Expand Up @@ -1650,6 +1685,42 @@ public void WebpushNotificationWithInvalidHttpsLinkUrl()
Assert.Throws<ArgumentException>(() => message.CopyAndValidate());
}

[Fact]
public void AnalyticsLabelInvalid()
{
var message = new Message()
{
Topic = "test-topic",
Notification = new Notification()
{
Title = "title",
Body = "body",
},
FcmOptions = new FcmOptions()
{
AnalyticsLabel = "label!",
RannyRanny marked this conversation as resolved.
Show resolved Hide resolved
},
};
var expected = new JObject()
{
{ "topic", "test-topic" },
{
"notification", new JObject()
{
{ "title", "title" },
{ "body", "body" },
}
},
{
"fcm_options", new JObject()
{
{ "analytics_label", "label" },
}
},
};
Assert.Throws<ArgumentException>(() => this.AssertJsonEquals(expected, message));
RannyRanny marked this conversation as resolved.
Show resolved Hide resolved
}

private void AssertJsonEquals(JObject expected, Message actual)
{
var json = NewtonsoftJsonSerializer.Instance.Serialize(actual.CopyAndValidate());
Expand Down
2 changes: 1 addition & 1 deletion FirebaseAdmin/FirebaseAdmin/FirebaseAdmin.csproj
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.7.0</Version>
Expand Down
7 changes: 7 additions & 0 deletions FirebaseAdmin/FirebaseAdmin/Messaging/AndroidConfig.cs
Expand Up @@ -66,6 +66,12 @@ public sealed class AndroidConfig
[JsonProperty("notification")]
public AndroidNotification Notification { get; set; }

/// <summary>
/// Gets or sets the FCM options to be included in the message.
/// </summary>
[JsonProperty("fcm_options")]
public AndroidFcmOptions FcmOptions { get; set; }

/// <summary>
/// Gets or sets the string representation of <see cref="Priority"/> as accepted by the FCM
/// backend service.
Expand Down Expand Up @@ -159,6 +165,7 @@ internal AndroidConfig CopyAndValidate()
TimeToLive = this.TimeToLive,
RestrictedPackageName = this.RestrictedPackageName,
Data = this.Data?.Copy(),
FcmOptions = this.FcmOptions?.CopyAndValidate(),
};
var totalSeconds = copy.TimeToLive?.TotalSeconds ?? 0;
if (totalSeconds < 0)
Expand Down
32 changes: 32 additions & 0 deletions FirebaseAdmin/FirebaseAdmin/Messaging/AndroidFcmOptions.cs
@@ -0,0 +1,32 @@
using FirebaseAdmin.Messaging.Util;
RannyRanny marked this conversation as resolved.
Show resolved Hide resolved
using Newtonsoft.Json;

namespace FirebaseAdmin.Messaging
{
/// <summary>
/// Represents Android FCM options.
/// </summary>
public sealed class AndroidFcmOptions
{
/// <summary>
/// Gets or sets analytics label.
/// </summary>
[JsonProperty("analytics_label")]
public string AnalyticsLabel { get; set; }

/// <summary>
/// Copies this FCM options, and validates the content of it to ensure that it can
/// be serialized into the JSON format expected by the FCM service.
/// </summary>
internal AndroidFcmOptions CopyAndValidate()
{
var copy = new AndroidFcmOptions()
{
AnalyticsLabel = this.AnalyticsLabel,
};
AnalyticsLabelChecker.CheckAnalyticsLabelOrThrow(this.AnalyticsLabel);
RannyRanny marked this conversation as resolved.
Show resolved Hide resolved

return copy;
}
}
}
7 changes: 7 additions & 0 deletions FirebaseAdmin/FirebaseAdmin/Messaging/ApnsConfig.cs
Expand Up @@ -35,6 +35,12 @@ public sealed class ApnsConfig
[JsonProperty("headers")]
public IReadOnlyDictionary<string, string> Headers { get; set; }

/// <summary>
/// Gets or sets the FCM options to be included in the message.
/// </summary>
[JsonProperty("fcm_options")]
public ApnsFcmOptions FcmOptions { get; set; }

/// <summary>
/// Gets or sets the <c>aps</c> dictionary to be included in the APNs payload.
/// </summary>
Expand Down Expand Up @@ -102,6 +108,7 @@ internal ApnsConfig CopyAndValidate()
{
Headers = this.Headers?.Copy(),
Payload = this.Payload.CopyAndValidate(),
FcmOptions = this.FcmOptions?.CopyAndValidate(),
};
return copy;
}
Expand Down
32 changes: 32 additions & 0 deletions FirebaseAdmin/FirebaseAdmin/Messaging/ApnsFcmOptions.cs
@@ -0,0 +1,32 @@
using FirebaseAdmin.Messaging.Util;
RannyRanny marked this conversation as resolved.
Show resolved Hide resolved
using Newtonsoft.Json;

namespace FirebaseAdmin.Messaging
{
/// <summary>
/// Represents Apple Push Notification Service FCM options.
/// </summary>
public sealed class ApnsFcmOptions
{
/// <summary>
/// Gets or sets analytics label.
/// </summary>
[JsonProperty("analytics_label")]
public string AnalyticsLabel { get; set; }

/// <summary>
/// Copies this FCM options, and validates the content of it to ensure that it can
/// be serialized into the JSON format expected by the FCM service.
/// </summary>
internal ApnsFcmOptions CopyAndValidate()
{
var copy = new ApnsFcmOptions()
{
AnalyticsLabel = this.AnalyticsLabel,
};
AnalyticsLabelChecker.CheckAnalyticsLabelOrThrow(this.AnalyticsLabel);
RannyRanny marked this conversation as resolved.
Show resolved Hide resolved

return copy;
}
}
}
32 changes: 32 additions & 0 deletions FirebaseAdmin/FirebaseAdmin/Messaging/FcmOptions.cs
@@ -0,0 +1,32 @@
using FirebaseAdmin.Messaging.Util;
RannyRanny marked this conversation as resolved.
Show resolved Hide resolved
using Newtonsoft.Json;

namespace FirebaseAdmin.Messaging
{
/// <summary>
/// Represents FCM options.
/// </summary>
public sealed class FcmOptions
{
/// <summary>
/// Gets or sets analytics label.
/// </summary>
[JsonProperty("analytics_label")]
public string AnalyticsLabel { get; set; }

/// <summary>
/// Copies this FCM options, and validates the content of it to ensure that it can
/// be serialized into the JSON format expected by the FCM service.
/// </summary>
internal FcmOptions CopyAndValidate()
{
var copy = new FcmOptions()
{
AnalyticsLabel = this.AnalyticsLabel,
};
AnalyticsLabelChecker.CheckAnalyticsLabelOrThrow(this.AnalyticsLabel);
RannyRanny marked this conversation as resolved.
Show resolved Hide resolved

return copy;
}
}
}
7 changes: 7 additions & 0 deletions FirebaseAdmin/FirebaseAdmin/Messaging/Message.cs
Expand Up @@ -80,6 +80,12 @@ public sealed class Message
[JsonProperty("apns")]
public ApnsConfig Apns { get; set; }

/// <summary>
/// Gets or sets the FCM options to be included in the message.
/// </summary>
[JsonProperty("fcm_options")]
public FcmOptions FcmOptions { get; set; }

/// <summary>
/// Gets or sets the formatted representation of the <see cref="Topic"/>. Removes the
/// <c>/topics/</c> prefix if present. This is what's ultimately sent to the FCM
Expand Down Expand Up @@ -119,6 +125,7 @@ internal Message CopyAndValidate()
Topic = this.Topic,
Condition = this.Condition,
Data = this.Data?.Copy(),
FcmOptions = this.FcmOptions?.CopyAndValidate(),
};
var list = new List<string>()
{
Expand Down
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text.RegularExpressions;

namespace FirebaseAdmin.Messaging.Util
{
/// <summary>
/// Checker for analytics label.
/// </summary>
public static class AnalyticsLabelChecker
RannyRanny marked this conversation as resolved.
Show resolved Hide resolved
{
private static ImmutableHashSet<char> alowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-~%".ToCharArray().ToImmutableHashSet();

/// <summary>
/// Checks anytytics labels an throw if not valid.
/// </summary>
/// <exception cref="ArgumentException">If analytics label does not match pattern.</exception>
/// <param name="analyticsLabel">Analytics label.</param>
public static void CheckAnalyticsLabelOrThrow(string analyticsLabel)
RannyRanny marked this conversation as resolved.
Show resolved Hide resolved
{
if (analyticsLabel.Length > 50)
RannyRanny marked this conversation as resolved.
Show resolved Hide resolved
{
throw new ArgumentException("Analytics label must have format matching'^[a-zA-Z0-9-_.~%]{1,50}$");
}

foreach (var character in analyticsLabel)
{
if (!alowedChars.Contains(character))
{
throw new ArgumentException("Analytics label must have format matching'^[a-zA-Z0-9-_.~%]{1,50}$");
}
}
}
}
}
RannyRanny marked this conversation as resolved.
Show resolved Hide resolved