Skip to content

AndroidNotification sends event_time = 0001-01-01 when EventTimestamp is not set #618

Backlog

Description

@caglarenes

[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

  1. Build an AndroidNotification without setting EventTimestamp. A typical reason to create
    one is to set ChannelId.
  2. Send the message with FirebaseMessaging.SendAsync (or serialize it as below).
  3. The request body contains event_time for 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

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

A non-breaking fix is proposed in #619: omit event_time when EventTimestamp is
default(DateTime).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions