Repository navigation
Description
[REQUIRED] Step 2: Describe your environment
- Operating System version: Linux containers (server). Also reproduced on Windows 11.
- Firebase SDK version: FirebaseAdmin 3.7.1. The code is unchanged on
main(01be477). - Firebase Product: messaging (FCM HTTP v1)
- .NET version: .NET 11 in the app. Also reproduced in the SDK's own unit tests on net6.0 and net462.
- OS: Android (receiving devices)
[REQUIRED] Step 3: Describe the problem
AndroidNotification.EventTimestamp is a non-nullable DateTime, and the SDK always serializes
it. If the caller never sets it, every Android notification goes out with
"event_time": "0001-01-01T00:00:00.000000000Z". FCM accepts this because it is a valid
google.protobuf.Timestamp. When the FCM Android SDK displays the notification, it uses
event_time as the notification's when. As a result, the notification shows a wrong
date/time instead of its arrival time, and the drawer sorts it as older than every other
notification.
The other optional members of the same class (Priority, Visibility, Proxy,
NotificationCount, VibrateTimingsMillis) are left out of the request when unset.
EventTimestamp is the only one that is not.
Steps to reproduce
- Build an
AndroidNotificationwithout settingEventTimestamp. A typical reason to create
one is to setChannelId. - Send the message with
FirebaseMessaging.SendAsync(or serialize it as below). - The request body contains
event_timefor year 0001.
Relevant Code
#:package FirebaseAdmin@3.7.1
using System;
using FirebaseAdmin.Messaging;
using Google.Apis.Json;
var message = new Message
{
Topic = "news",
Notification = new Notification { Title = "Hello", Body = "World" },
Android = new AndroidConfig
{
Notification = new AndroidNotification { ChannelId = "default" },
},
};
// FirebaseMessagingClient uses this serializer for the send request body.
Console.WriteLine(NewtonsoftJsonSerializer.Instance.Serialize(message));Output with 3.7.1 (formatted):
{
"notification": { "title": "Hello", "body": "World" },
"android": {
"notification": {
"channel_id": "default",
"sticky": false,
"local_only": false,
"default_vibrate_timings": false,
"default_sound": false,
"default_light_settings": false,
"event_time": "0001-01-01T00:00:00.000000000Z"
}
},
"topic": "news"
}On hosts west of UTC, the time of day shifts. For example, a host at UTC-5 sends
0001-01-01T05:00:00.000000000Z, because the unset value has DateTimeKind.Unspecified and goes
through ToUniversalTime().
Expected
When EventTimestamp is not set, event_time is left out of the request, and the device uses the
time it posted the notification. This is how the other Admin SDKs behave. Go uses
EventTimestamp *time.Time and omits nil.
Node uses
eventTimestamp?: Date.
Java and Python leave the field unset by default.
Actual
event_time is always sent, with the year-0001 value when it was never set.
Root cause
EventTimestampis a non-nullableDateTime
(v3.7.1).- The
EventTimeStringgetter always returns a formatted string,
so the serializer's null-value handling never drops it. - In contrast, the optional members of the same class are nullable:
Priority?,
Visibility?,
NotificationCount?
andProxy?.
Their string helpers
returnnullwhen unset,
andNewtonsoftJsonSerializeromits them. FirebaseMessagingClientserializesCopyAndValidate()output with that serializer.- On the device,
the FCM Android SDK callssetShowWhen(true)andsetWhen(eventTime)wheneverevent_timeis present.
Workaround
Set EventTimestamp on every AndroidNotification, either to the real event time or to
DateTime.UtcNow when sending:
Notification = new AndroidNotification { ChannelId = "default", EventTimestamp = DateTime.UtcNow },If no Android-specific notification options are needed, you can instead leave
AndroidConfig.Notification null.
Related
- Add latest HTTP v1 options #153 / Closes #153. Add additional FCM options for Android Notification #203 added
EventTimestamp(2.3.0). - Invalid value at 'message.android.notification.event_time'. When im trying to send an AndroidNotification but keep getting this exception #330, Sending notifications using C# FirebaseAdmin 2.4.0 doesn't work. Invalid value at 'message.android.notification.event_time' #370, Push Notification Error Invalid value at 'message.android.notification.event_time' #381 and Incorrect serialization of the event_time field on Italian systems #408 (fixed by fix(fcm): Use Invariant Culture when generating event_time #417) were about locale-dependent formatting of the same
field. They also show thatevent_timeis sent even when the caller never sets it, but none of
them reports the year-0001 value. - Open PR feat(fcm): Add support for AndroidConfigV2 #537 adds
AndroidNotificationV2withDateTime? EventTimestamp, which is omitted when
null. It marksAndroidNotificationobsolete but keeps its behavior unchanged, so the legacy
class keeps sending year 0001 for callers that have not migrated.
A non-breaking fix is proposed in #619: omit event_time when EventTimestamp is
default(DateTime).
Activity