From e1c5b2a83b69c407866453f63425d5a55f937b32 Mon Sep 17 00:00:00 2001 From: Jonathan Edey Date: Tue, 18 Mar 2025 14:26:52 -0400 Subject: [PATCH] feat(fcm): Support `proxy` field in FCM `AndroidNotification` --- .../Messaging/MessageTest.cs | 4 ++ .../Messaging/AndroidNotification.cs | 50 ++++++++++++++++++- .../Messaging/NotificationProxy.cs | 38 ++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 FirebaseAdmin/FirebaseAdmin/Messaging/NotificationProxy.cs diff --git a/FirebaseAdmin/FirebaseAdmin.Tests/Messaging/MessageTest.cs b/FirebaseAdmin/FirebaseAdmin.Tests/Messaging/MessageTest.cs index fbf190a9..a83647f9 100644 --- a/FirebaseAdmin/FirebaseAdmin.Tests/Messaging/MessageTest.cs +++ b/FirebaseAdmin/FirebaseAdmin.Tests/Messaging/MessageTest.cs @@ -293,6 +293,7 @@ public void AndroidConfig() DefaultLightSettings = false, Visibility = NotificationVisibility.PUBLIC, NotificationCount = 10, + Proxy = NotificationProxy.IfPriorityLowered, }, FcmOptions = new AndroidFcmOptions() { @@ -359,6 +360,7 @@ public void AndroidConfig() { "visibility", "PUBLIC" }, { "vibrate_timings", new JArray() { "1s", "1.001000000s" } }, { "event_time", "2020-06-27T20:29:06.032691000Z" }, + { "proxy", "IF_PRIORITY_LOWERED" }, } }, { @@ -493,6 +495,7 @@ public void AndroidNotificationDeserialization() DefaultLightSettings = false, Visibility = NotificationVisibility.PUBLIC, NotificationCount = 10, + Proxy = NotificationProxy.IfPriorityLowered, }; var json = NewtonsoftJsonSerializer.Instance.Serialize(original); var copy = NewtonsoftJsonSerializer.Instance.Deserialize(json); @@ -523,6 +526,7 @@ public void AndroidNotificationDeserialization() Assert.Equal(original.DefaultLightSettings, copy.DefaultLightSettings); Assert.Equal(original.Visibility, copy.Visibility); Assert.Equal(original.NotificationCount, copy.NotificationCount); + Assert.Equal(original.Proxy, copy.Proxy); } [Fact] diff --git a/FirebaseAdmin/FirebaseAdmin/Messaging/AndroidNotification.cs b/FirebaseAdmin/FirebaseAdmin/Messaging/AndroidNotification.cs index 5c1832ce..19084a05 100644 --- a/FirebaseAdmin/FirebaseAdmin/Messaging/AndroidNotification.cs +++ b/FirebaseAdmin/FirebaseAdmin/Messaging/AndroidNotification.cs @@ -224,6 +224,12 @@ public sealed class AndroidNotification [JsonProperty("notification_count")] public int? NotificationCount { get; set; } + /// + /// Gets or sets the proxy behavior of this notification. + /// + [JsonIgnore] + public NotificationProxy? Proxy { get; set; } + /// /// Gets or sets the string representation of the property. /// @@ -312,7 +318,7 @@ private string VisibilityString return; default: throw new ArgumentException( - $"Invalid visibility value: {value}. Only 'PUBLIC', 'PRIVATE', ''SECRET' are allowed."); + $"Invalid visibility value: {value}. Only 'PUBLIC', 'PRIVATE', 'SECRET' are allowed."); } } } @@ -379,6 +385,47 @@ private string EventTimeString } } + /// + /// Gets or sets the string representation of the property. + /// + [JsonProperty("proxy")] + private string ProxyString + { + get + { + switch (this.Proxy) + { + case NotificationProxy.Allow: + return "ALLOW"; + case NotificationProxy.Deny: + return "DENY"; + case NotificationProxy.IfPriorityLowered: + return "IF_PRIORITY_LOWERED"; + default: + return null; + } + } + + set + { + switch (value) + { + case "ALLOW": + this.Proxy = NotificationProxy.Allow; + return; + case "DENY": + this.Proxy = NotificationProxy.Deny; + return; + case "IF_PRIORITY_LOWERED": + this.Proxy = NotificationProxy.IfPriorityLowered; + return; + default: + throw new ArgumentException( + $"Invalid proxy value: {value}. Only 'ALLOW', 'DENY', 'IF_PRIORITY_LOWERED' are allowed."); + } + } + } + /// /// Copies this notification, and validates the content of it to ensure that it can be /// serialized into the JSON format expected by the FCM service. @@ -411,6 +458,7 @@ internal AndroidNotification CopyAndValidate() DefaultLightSettings = this.DefaultLightSettings, Visibility = this.Visibility, NotificationCount = this.NotificationCount, + Proxy = this.Proxy, }; if (copy.Color != null && !Regex.Match(copy.Color, "^#[0-9a-fA-F]{6}$").Success) { diff --git a/FirebaseAdmin/FirebaseAdmin/Messaging/NotificationProxy.cs b/FirebaseAdmin/FirebaseAdmin/Messaging/NotificationProxy.cs new file mode 100644 index 00000000..f0c4252f --- /dev/null +++ b/FirebaseAdmin/FirebaseAdmin/Messaging/NotificationProxy.cs @@ -0,0 +1,38 @@ +// Copyright 2025, Google Inc. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace FirebaseAdmin.Messaging +{ + /// + /// Proxy behavior that can be set on an . + /// + public enum NotificationProxy + { + /// + /// Try to proxy this notification. + /// + Allow, + + /// + /// Do not proxy this notification.. + /// + Deny, + + /// + /// Only try to proxy this notification if its `AndroidMessagePriority` + /// was lowered from HIGH to NORMAL on the device. + /// + IfPriorityLowered, + } +}