Skip to content

Commit 32821c0

Browse files
committed
Update API to match the approved changes
1 parent 0547948 commit 32821c0

File tree

5 files changed

+124
-77
lines changed

5 files changed

+124
-77
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ obj/
33
.vscode/
44
.vs/
55
.idea/
6+
.DS_Store

FirebaseAdmin/FirebaseAdmin.Tests/Messaging/MessageTest.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,9 @@ public void AndroidConfig()
282282
VibrateTimingsMillis = new long[] { 1000L, 1001L },
283283
DefaultVibrateTimings = false,
284284
DefaultSound = true,
285-
LightSettings = new LightSettings
285+
LightSettings = new LightSettings()
286286
{
287-
Color = new LightSettingsColor { Red = 0.2f, Green = 0.4f, Blue = 0.6f },
287+
Color = "#aabbccdd",
288288
LightOnDurationMillis = 1002L,
289289
LightOffDurationMillis = 1003L,
290290
},
@@ -333,7 +333,17 @@ public void AndroidConfig()
333333
{
334334
"light_settings", new JObject()
335335
{
336-
{ "color", "#336699" }, { "light_on_duration", "1.002000000s" }, { "light_off_duration", "1.003000000s" },
336+
{ "light_on_duration", "1.002000000s" },
337+
{ "light_off_duration", "1.003000000s" },
338+
{
339+
"color", new JObject()
340+
{
341+
{ "red", 0.6666667 },
342+
{ "green", 0.733333349 },
343+
{ "blue", 0.8 },
344+
{ "alpha", 0.8666667 },
345+
}
346+
},
337347
}
338348
},
339349
{ "default_light_settings", false },
@@ -465,9 +475,9 @@ public void AndroidNotificationDeserialization()
465475
VibrateTimingsMillis = new long[] { 1000L, 1001L },
466476
DefaultVibrateTimings = false,
467477
DefaultSound = true,
468-
LightSettings = new LightSettings
478+
LightSettings = new LightSettings()
469479
{
470-
Color = new LightSettingsColor { Red = 0.2F, Green = 0.4F, Blue = 0.6F, Alpha = 1.0F },
480+
Color = "#AABBCCDD",
471481
LightOnDurationMillis = 1002L,
472482
LightOffDurationMillis = 1003L,
473483
},
@@ -498,10 +508,9 @@ public void AndroidNotificationDeserialization()
498508
Assert.Equal(original.VibrateTimingsMillis, copy.VibrateTimingsMillis);
499509
Assert.Equal(original.DefaultVibrateTimings, copy.DefaultVibrateTimings);
500510
Assert.Equal(original.DefaultSound, copy.DefaultSound);
501-
Assert.Equal(original.LightSettings.Color.Red, copy.LightSettings.Color.Red);
502-
Assert.Equal(original.LightSettings.Color.Blue, copy.LightSettings.Color.Blue);
503-
Assert.Equal(original.LightSettings.Color.Green, copy.LightSettings.Color.Green);
504-
Assert.Equal(original.LightSettings.Color.Alpha, copy.LightSettings.Color.Alpha);
511+
Assert.Equal(original.LightSettings.Color, copy.LightSettings.Color);
512+
Assert.Equal(original.LightSettings.LightOnDurationMillis, copy.LightSettings.LightOnDurationMillis);
513+
Assert.Equal(original.LightSettings.LightOffDurationMillis, copy.LightSettings.LightOffDurationMillis);
505514
Assert.Equal(original.DefaultLightSettings, copy.DefaultLightSettings);
506515
Assert.Equal(original.Visibility, copy.Visibility);
507516
Assert.Equal(original.NotificationCount, copy.NotificationCount);

FirebaseAdmin/FirebaseAdmin/Messaging/AndroidNotification.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
using System.Linq;
1919
using System.Text.RegularExpressions;
2020
using FirebaseAdmin.Messaging.Util;
21-
using Google.Apis.Util;
2221
using Newtonsoft.Json;
2322

2423
namespace FirebaseAdmin.Messaging
@@ -461,7 +460,6 @@ internal AndroidNotification CopyAndValidate()
461460
VibrateTimingsMillis = this.VibrateTimingsMillis,
462461
DefaultVibrateTimings = this.DefaultVibrateTimings,
463462
DefaultSound = this.DefaultSound,
464-
LightSettings = this.LightSettings,
465463
DefaultLightSettings = this.DefaultLightSettings,
466464
Visibility = this.Visibility,
467465
NotificationCount = this.NotificationCount,
@@ -486,6 +484,8 @@ internal AndroidNotification CopyAndValidate()
486484
throw new ArgumentException($"Malformed image URL string: {copy.ImageUrl}.");
487485
}
488486

487+
copy.LightSettings = this.LightSettings?.CopyAndValidate();
488+
489489
return copy;
490490
}
491491
}

FirebaseAdmin/FirebaseAdmin/Messaging/LightSettings.cs

Lines changed: 103 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,24 @@ namespace FirebaseAdmin.Messaging
2525
/// </summary>
2626
public sealed class LightSettings
2727
{
28+
private LightSettingsColor lightColor = new LightSettingsColor();
29+
2830
/// <summary>
29-
/// Gets or sets the lightSettingsColor value in the light settings.
31+
/// Gets or sets the color value of the light settings.
3032
/// </summary>
3133
[JsonIgnore]
32-
public LightSettingsColor Color { get; set; }
34+
public string Color
35+
{
36+
get
37+
{
38+
return this.lightColor.ColorString();
39+
}
40+
41+
set
42+
{
43+
this.lightColor = LightSettingsColor.FromString(value);
44+
}
45+
}
3346

3447
/// <summary>
3548
/// Gets or sets the light on duration in milliseconds.
@@ -44,45 +57,19 @@ public sealed class LightSettings
4457
public long LightOffDurationMillis { get; set; }
4558

4659
/// <summary>
47-
/// Gets or sets a string representation of <see cref="LightSettingsColor"/>.
60+
/// Gets or sets the light settings color representation as accepted by the FCM backend service.
4861
/// </summary>
4962
[JsonProperty("color")]
50-
private string LightSettingsColorString
63+
private LightSettingsColor LightColor
5164
{
5265
get
5366
{
54-
var colorStringBuilder = new StringBuilder();
55-
56-
colorStringBuilder
57-
.Append("#")
58-
.Append(Convert.ToInt32(this.Color.Red * 255).ToString("X"))
59-
.Append(Convert.ToInt32(this.Color.Green * 255).ToString("X"))
60-
.Append(Convert.ToInt32(this.Color.Blue * 255).ToString("X"));
61-
62-
return colorStringBuilder.ToString();
67+
return this.lightColor;
6368
}
6469

6570
set
6671
{
67-
var pattern = new Regex("^#[0-9a-fA-F]{6}$");
68-
69-
if (string.IsNullOrEmpty(value))
70-
{
71-
throw new ArgumentException("Invalid LightSettingsColor. LightSettingsColor annot be null or empty");
72-
}
73-
74-
if (!pattern.IsMatch(value))
75-
{
76-
throw new ArgumentException($"Invalid LightSettingsColor {value}. LightSettingsColor must be in the form #RRGGBB");
77-
}
78-
79-
this.Color = new LightSettingsColor
80-
{
81-
Red = Convert.ToInt32(value.Substring(1, 2), 16) / 255.0f,
82-
Green = Convert.ToInt32(value.Substring(3, 2), 16) / 255.0f,
83-
Blue = Convert.ToInt32(value.Substring(5, 2), 16) / 255.0f,
84-
Alpha = 1.0f,
85-
};
72+
this.lightColor = value;
8673
}
8774
}
8875

@@ -119,5 +106,89 @@ private string LightOffDurationMillisString
119106
this.LightOffDurationMillis = TimeConverter.StringToLongMillis(value);
120107
}
121108
}
109+
110+
/// <summary>
111+
/// Copies this Light Settings, and validates the content of it to ensure that it can be
112+
/// serialized into the JSON format expected by the FCM service.
113+
/// </summary>
114+
internal LightSettings CopyAndValidate()
115+
{
116+
// Copy and validate the leaf-level properties
117+
var copy = new LightSettings()
118+
{
119+
Color = this.Color,
120+
LightOnDurationMillis = this.LightOnDurationMillis,
121+
LightOffDurationMillis = this.LightOffDurationMillis,
122+
};
123+
124+
return copy;
125+
}
126+
127+
/// <summary>
128+
/// The LightSettings Color object as expected by the FCM backend service.
129+
/// </summary>
130+
private class LightSettingsColor
131+
{
132+
/// <summary>
133+
/// Gets or sets the red component.
134+
/// </summary>
135+
[JsonProperty("red")]
136+
internal float Red { get; set; }
137+
138+
/// <summary>
139+
/// Gets or sets the green component.
140+
/// </summary>
141+
[JsonProperty("green")]
142+
internal float Green { get; set; }
143+
144+
/// <summary>
145+
/// Gets or sets the blue component.
146+
/// </summary>
147+
[JsonProperty("blue")]
148+
internal float Blue { get; set; }
149+
150+
/// <summary>
151+
/// Gets or sets the alpha component.
152+
/// </summary>
153+
[JsonProperty("alpha")]
154+
internal float Alpha { get; set; }
155+
156+
internal static LightSettingsColor FromString(string color)
157+
{
158+
if (string.IsNullOrEmpty(color))
159+
{
160+
throw new ArgumentException("Light settings color must not be null or empty");
161+
}
162+
163+
if (!Regex.Match(color, "^#[0-9a-fA-F]{6}$").Success && !Regex.Match(color, "^#[0-9a-fA-F]{8}$").Success)
164+
{
165+
throw new ArgumentException($"Invalid Light Settings Color {color}. Must be in the form of #RRGGBB or #RRGGBBAA.");
166+
}
167+
168+
var colorString = color.Length == 7 ? color + "FF" : color;
169+
170+
return new LightSettingsColor()
171+
{
172+
Red = Convert.ToInt32(colorString.Substring(1, 2), 16) / 255.0f,
173+
Green = Convert.ToInt32(colorString.Substring(3, 2), 16) / 255.0f,
174+
Blue = Convert.ToInt32(colorString.Substring(5, 2), 16) / 255.0f,
175+
Alpha = Convert.ToInt32(colorString.Substring(7, 2), 16) / 255.0f,
176+
};
177+
}
178+
179+
internal string ColorString()
180+
{
181+
var colorStringBuilder = new StringBuilder();
182+
183+
colorStringBuilder
184+
.Append("#")
185+
.Append(Convert.ToInt32(this.Red * 255).ToString("X"))
186+
.Append(Convert.ToInt32(this.Green * 255).ToString("X"))
187+
.Append(Convert.ToInt32(this.Blue * 255).ToString("X"))
188+
.Append(Convert.ToInt32(this.Alpha * 255).ToString("X"));
189+
190+
return colorStringBuilder.ToString();
191+
}
192+
}
122193
}
123194
}

FirebaseAdmin/FirebaseAdmin/Messaging/LightSettingsColor.cs

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)