Skip to content

Commit 48659aa

Browse files
authored
Merge pull request #89 from RannyRanny/support_for_analytics_label
Added support for analytics_label
2 parents 869478c + 10c0b16 commit 48659aa

File tree

12 files changed

+346
-2
lines changed

12 files changed

+346
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ bin/
22
obj/
33
.vscode/
44
.vs/
5+
.idea/

FirebaseAdmin/FirebaseAdmin.Tests/FirebaseAdmin.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.0</TargetFramework>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright 2019, Google Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System;
16+
using FirebaseAdmin.Messaging;
17+
using FirebaseAdmin.Messaging.Util;
18+
using Xunit;
19+
20+
namespace FirebaseAdmin.Tests.Messaging
21+
{
22+
public class FcmOptionsTest
23+
{
24+
[Fact]
25+
public void FcmOptionsCopyAndValidate()
26+
{
27+
var options = new FcmOptions() { AnalyticsLabel = "label" };
28+
var result = options.CopyAndValidate();
29+
Assert.Equal(options.AnalyticsLabel, result.AnalyticsLabel);
30+
}
31+
32+
[Fact]
33+
public void ApnsFcmOptionsCopyAndValidate()
34+
{
35+
var options = new ApnsFcmOptions() { AnalyticsLabel = "label" };
36+
var result = options.CopyAndValidate();
37+
Assert.Equal(options.AnalyticsLabel, result.AnalyticsLabel);
38+
}
39+
40+
[Fact]
41+
public void AndroidFcmOptionsCopyAndValidate()
42+
{
43+
var options = new AndroidFcmOptions() { AnalyticsLabel = "label" };
44+
var result = options.CopyAndValidate();
45+
Assert.Equal(options.AnalyticsLabel, result.AnalyticsLabel);
46+
}
47+
48+
[Fact]
49+
public void FcmOptionsCopyAndValidateNullLabel()
50+
{
51+
var options = new FcmOptions() { AnalyticsLabel = null };
52+
options.CopyAndValidate();
53+
}
54+
55+
[Fact]
56+
public void FcmOptionsCopyAndValidateEmptyLabel()
57+
{
58+
var options = new FcmOptions() { AnalyticsLabel = string.Empty };
59+
Assert.Throws<ArgumentException>(() => options.CopyAndValidate());
60+
}
61+
62+
[Fact]
63+
public void AnalyticsLabelTooLong()
64+
{
65+
Assert.Throws<ArgumentException>(() => AnalyticsLabelChecker.ValidateAnalyticsLabel(new string('a', 51)));
66+
}
67+
68+
[Fact]
69+
public void AnalyticsLabelEmtpty()
70+
{
71+
Assert.Throws<ArgumentException>(() => AnalyticsLabelChecker.ValidateAnalyticsLabel(string.Empty));
72+
}
73+
74+
[Fact]
75+
public void AnalyticsLabelInvalidCharacters()
76+
{
77+
Assert.Throws<ArgumentException>(() => AnalyticsLabelChecker.ValidateAnalyticsLabel("label(label)"));
78+
}
79+
}
80+
}

FirebaseAdmin/FirebaseAdmin.Tests/Messaging/MessageTest.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ public void Notification()
7575
Title = "title",
7676
Body = "body",
7777
},
78+
FcmOptions = new FcmOptions()
79+
{
80+
AnalyticsLabel = "label",
81+
},
7882
};
7983
var expected = new JObject()
8084
{
@@ -86,6 +90,12 @@ public void Notification()
8690
{ "body", "body" },
8791
}
8892
},
93+
{
94+
"fcm_options", new JObject()
95+
{
96+
{ "analytics_label", "label" },
97+
}
98+
},
8999
};
90100
this.AssertJsonEquals(expected, message);
91101
}
@@ -117,6 +127,10 @@ public void MessageDeserialization()
117127
{
118128
Data = new Dictionary<string, string>() { { "key", "value" } },
119129
},
130+
FcmOptions = new FcmOptions()
131+
{
132+
AnalyticsLabel = "label",
133+
},
120134
};
121135
var json = NewtonsoftJsonSerializer.Instance.Serialize(original);
122136
var copy = NewtonsoftJsonSerializer.Instance.Deserialize<Message>(json);
@@ -128,6 +142,7 @@ public void MessageDeserialization()
128142
original.Android.RestrictedPackageName, copy.Android.RestrictedPackageName);
129143
Assert.Equal(original.Apns.Aps.AlertString, copy.Apns.Aps.AlertString);
130144
Assert.Equal(original.Webpush.Data, copy.Webpush.Data);
145+
Assert.Equal(original.FcmOptions.AnalyticsLabel, copy.FcmOptions.AnalyticsLabel);
131146
}
132147

133148
[Fact]
@@ -236,6 +251,10 @@ public void AndroidConfig()
236251
BodyLocArgs = new List<string>() { "arg3", "arg4" },
237252
ChannelId = "channel-id",
238253
},
254+
FcmOptions = new AndroidFcmOptions()
255+
{
256+
AnalyticsLabel = "label",
257+
},
239258
},
240259
};
241260
var expected = new JObject()
@@ -266,6 +285,12 @@ public void AndroidConfig()
266285
{ "channel_id", "channel-id" },
267286
}
268287
},
288+
{
289+
"fcm_options", new JObject()
290+
{
291+
{ "analytics_label", "label" },
292+
}
293+
},
269294
}
270295
},
271296
};
@@ -843,6 +868,10 @@ public void ApnsConfig()
843868
{ "custom-key3", "custom-data" },
844869
{ "custom-key4", true },
845870
},
871+
FcmOptions = new ApnsFcmOptions()
872+
{
873+
AnalyticsLabel = "label",
874+
},
846875
},
847876
};
848877
var expected = new JObject()
@@ -879,6 +908,12 @@ public void ApnsConfig()
879908
{ "custom-key4", true },
880909
}
881910
},
911+
{
912+
"fcm_options", new JObject()
913+
{
914+
{ "analytics_label", "label" },
915+
}
916+
},
882917
}
883918
},
884919
};
@@ -1650,6 +1685,25 @@ public void WebpushNotificationWithInvalidHttpsLinkUrl()
16501685
Assert.Throws<ArgumentException>(() => message.CopyAndValidate());
16511686
}
16521687

1688+
[Fact]
1689+
public void AnalyticsLabelInvalid()
1690+
{
1691+
var message = new Message()
1692+
{
1693+
Topic = "test-topic",
1694+
Notification = new Notification()
1695+
{
1696+
Title = "title",
1697+
Body = "body",
1698+
},
1699+
FcmOptions = new FcmOptions()
1700+
{
1701+
AnalyticsLabel = "label!",
1702+
},
1703+
};
1704+
Assert.Throws<ArgumentException>(() => message.CopyAndValidate());
1705+
}
1706+
16531707
private void AssertJsonEquals(JObject expected, Message actual)
16541708
{
16551709
var json = NewtonsoftJsonSerializer.Instance.Serialize(actual.CopyAndValidate());

FirebaseAdmin/FirebaseAdmin/FirebaseAdmin.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<Version>1.7.0</Version>

FirebaseAdmin/FirebaseAdmin/Messaging/AndroidConfig.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ public sealed class AndroidConfig
6666
[JsonProperty("notification")]
6767
public AndroidNotification Notification { get; set; }
6868

69+
/// <summary>
70+
/// Gets or sets the FCM options to be included in the message.
71+
/// </summary>
72+
[JsonProperty("fcm_options")]
73+
public AndroidFcmOptions FcmOptions { get; set; }
74+
6975
/// <summary>
7076
/// Gets or sets the string representation of <see cref="Priority"/> as accepted by the FCM
7177
/// backend service.
@@ -159,6 +165,7 @@ internal AndroidConfig CopyAndValidate()
159165
TimeToLive = this.TimeToLive,
160166
RestrictedPackageName = this.RestrictedPackageName,
161167
Data = this.Data?.Copy(),
168+
FcmOptions = this.FcmOptions?.CopyAndValidate(),
162169
};
163170
var totalSeconds = copy.TimeToLive?.TotalSeconds ?? 0;
164171
if (totalSeconds < 0)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2019, Google Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using FirebaseAdmin.Messaging.Util;
16+
using Newtonsoft.Json;
17+
18+
namespace FirebaseAdmin.Messaging
19+
{
20+
/// <summary>
21+
/// Represents Android FCM options.
22+
/// </summary>
23+
public sealed class AndroidFcmOptions
24+
{
25+
/// <summary>
26+
/// Gets or sets analytics label.
27+
/// </summary>
28+
[JsonProperty("analytics_label")]
29+
public string AnalyticsLabel { get; set; }
30+
31+
/// <summary>
32+
/// Copies this FCM options, and validates the content of it to ensure that it can
33+
/// be serialized into the JSON format expected by the FCM service.
34+
/// </summary>
35+
internal AndroidFcmOptions CopyAndValidate()
36+
{
37+
var copy = new AndroidFcmOptions()
38+
{
39+
AnalyticsLabel = this.AnalyticsLabel,
40+
};
41+
AnalyticsLabelChecker.ValidateAnalyticsLabel(copy.AnalyticsLabel);
42+
43+
return copy;
44+
}
45+
}
46+
}

FirebaseAdmin/FirebaseAdmin/Messaging/ApnsConfig.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ public sealed class ApnsConfig
3535
[JsonProperty("headers")]
3636
public IReadOnlyDictionary<string, string> Headers { get; set; }
3737

38+
/// <summary>
39+
/// Gets or sets the FCM options to be included in the message.
40+
/// </summary>
41+
[JsonProperty("fcm_options")]
42+
public ApnsFcmOptions FcmOptions { get; set; }
43+
3844
/// <summary>
3945
/// Gets or sets the <c>aps</c> dictionary to be included in the APNs payload.
4046
/// </summary>
@@ -102,6 +108,7 @@ internal ApnsConfig CopyAndValidate()
102108
{
103109
Headers = this.Headers?.Copy(),
104110
Payload = this.Payload.CopyAndValidate(),
111+
FcmOptions = this.FcmOptions?.CopyAndValidate(),
105112
};
106113
return copy;
107114
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2019, Google Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using FirebaseAdmin.Messaging.Util;
16+
using Newtonsoft.Json;
17+
18+
namespace FirebaseAdmin.Messaging
19+
{
20+
/// <summary>
21+
/// Represents Apple Push Notification Service FCM options.
22+
/// </summary>
23+
public sealed class ApnsFcmOptions
24+
{
25+
/// <summary>
26+
/// Gets or sets analytics label.
27+
/// </summary>
28+
[JsonProperty("analytics_label")]
29+
public string AnalyticsLabel { get; set; }
30+
31+
/// <summary>
32+
/// Copies this FCM options, and validates the content of it to ensure that it can
33+
/// be serialized into the JSON format expected by the FCM service.
34+
/// </summary>
35+
internal ApnsFcmOptions CopyAndValidate()
36+
{
37+
var copy = new ApnsFcmOptions()
38+
{
39+
AnalyticsLabel = this.AnalyticsLabel,
40+
};
41+
AnalyticsLabelChecker.ValidateAnalyticsLabel(copy.AnalyticsLabel);
42+
43+
return copy;
44+
}
45+
}
46+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2019, Google Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using FirebaseAdmin.Messaging.Util;
16+
using Newtonsoft.Json;
17+
18+
namespace FirebaseAdmin.Messaging
19+
{
20+
/// <summary>
21+
/// Represents FCM options.
22+
/// </summary>
23+
public sealed class FcmOptions
24+
{
25+
/// <summary>
26+
/// Gets or sets analytics label.
27+
/// </summary>
28+
[JsonProperty("analytics_label")]
29+
public string AnalyticsLabel { get; set; }
30+
31+
/// <summary>
32+
/// Copies this FCM options, and validates the content of it to ensure that it can
33+
/// be serialized into the JSON format expected by the FCM service.
34+
/// </summary>
35+
internal FcmOptions CopyAndValidate()
36+
{
37+
var copy = new FcmOptions()
38+
{
39+
AnalyticsLabel = this.AnalyticsLabel,
40+
};
41+
AnalyticsLabelChecker.ValidateAnalyticsLabel(copy.AnalyticsLabel);
42+
43+
return copy;
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)