From 28728dd2bb616300cf687548c94b91567b1402d6 Mon Sep 17 00:00:00 2001 From: Enes Caglar Date: Sun, 29 Nov 2026 01:22:16 +0300 Subject: [PATCH] fix(fcm): Omit event_time when AndroidNotification.EventTimestamp is not set AndroidNotification.EventTimestamp is a non-nullable DateTime and the private EventTimeString helper always returned a formatted value, so a notification that never set EventTimestamp was sent with "event_time": "0001-01-01T00:00:00.000000000Z" (shifted by the host's UTC offset on hosts west of UTC). The Android FCM SDK uses event_time as the notification's "when" value, so these notifications show a wrong time and sort as the oldest entry in the notification drawer. EventTimeString now returns null when EventTimestamp is default(DateTime), and the serializer omits null values, so event_time is left out of the request. This matches how Priority, Visibility, Proxy and VibrateTimingsMillis are omitted when unset. The public API does not change. Testing: Added unit tests in MessageTest.cs that serialize an AndroidNotification with and without EventTimestamp. --- .../Messaging/MessageTest.cs | 76 +++++++++++++++++++ .../Messaging/AndroidNotification.cs | 8 +- 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/FirebaseAdmin/FirebaseAdmin.Tests/Messaging/MessageTest.cs b/FirebaseAdmin/FirebaseAdmin.Tests/Messaging/MessageTest.cs index b496067d..518c8a4d 100644 --- a/FirebaseAdmin/FirebaseAdmin.Tests/Messaging/MessageTest.cs +++ b/FirebaseAdmin/FirebaseAdmin.Tests/Messaging/MessageTest.cs @@ -526,6 +526,82 @@ public void AndroidConfigFullSecondsTTL() this.AssertJsonEquals(expected, message); } + [Fact] + public void AndroidNotificationWithoutEventTimestamp() + { + var message = new Message() + { + Topic = "test-topic", + Android = new AndroidConfig() + { + Notification = new AndroidNotification() + { + Title = "title", + }, + }, + }; + var expected = new JObject() + { + { "topic", "test-topic" }, + { + "android", new JObject() + { + { + "notification", new JObject() + { + { "title", "title" }, + { "sticky", false }, + { "local_only", false }, + { "default_vibrate_timings", false }, + { "default_sound", false }, + { "default_light_settings", false }, + } + }, + } + }, + }; + this.AssertJsonEquals(expected, message); + } + + [Fact] + public void AndroidNotificationWithEventTimestamp() + { + var message = new Message() + { + Topic = "test-topic", + Android = new AndroidConfig() + { + Notification = new AndroidNotification() + { + Title = "title", + EventTimestamp = new DateTime(2020, 6, 27, 20, 29, 6, DateTimeKind.Utc), + }, + }, + }; + var expected = new JObject() + { + { "topic", "test-topic" }, + { + "android", new JObject() + { + { + "notification", new JObject() + { + { "title", "title" }, + { "sticky", false }, + { "local_only", false }, + { "default_vibrate_timings", false }, + { "default_sound", false }, + { "default_light_settings", false }, + { "event_time", "2020-06-27T20:29:06.000000000Z" }, + } + }, + } + }, + }; + this.AssertJsonEquals(expected, message); + } + [Fact] public void AndroidConfigDeserialization() { diff --git a/FirebaseAdmin/FirebaseAdmin/Messaging/AndroidNotification.cs b/FirebaseAdmin/FirebaseAdmin/Messaging/AndroidNotification.cs index 19084a05..a6e22f6b 100644 --- a/FirebaseAdmin/FirebaseAdmin/Messaging/AndroidNotification.cs +++ b/FirebaseAdmin/FirebaseAdmin/Messaging/AndroidNotification.cs @@ -139,7 +139,8 @@ public sealed class AndroidNotification /// /// Gets or sets the time that the event in the notification occurred for notifications /// that inform users about events with an absolute time reference. Notifications in the panel - /// are sorted by this time. + /// are sorted by this time. When left at its default value, the event time is not included + /// in the message. /// [JsonIgnore] public DateTime EventTimestamp { get; set; } @@ -371,6 +372,11 @@ private string EventTimeString { get { + if (this.EventTimestamp == default(DateTime)) + { + return null; + } + return this.EventTimestamp.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ss.ffffff000'Z'", CultureInfo.InvariantCulture); }