firebase-admin-dotnet icon indicating copy to clipboard operation
firebase-admin-dotnet copied to clipboard

Push Notification Error Invalid value at 'message.android.notification.event_time'

Open furofo opened this issue 1 year ago • 8 comments

[REQUIRED] Step 2: Describe your environment

  • Operating System version:Build 19045.3930
  • Firebase SDK version: 2.4.0
  • Firebase Product: FirebaseAdmin Nuget Package .NET 2.4.0
  • .NET version: 8.0.200
  • OS: Windows 10 Enterprise

[REQUIRED] Step 3: Describe the problem

Issues with the following error message:

FirebaseAdmin.Messaging.FirebaseMessagingException: Invalid value at 'message.android.notification.event_time' (type.googleapis.com/google.protobuf.Timestamp), Field 'event_time', Invalid time format: Failed to parse input

It occurs when trying to use Firebase SDK 2.4.0 in .NET and sending an Android notification when you have your system time set to a format that is not U.S.-based, like Europe. Currently, if your time settings are set to a region with a different time format, like Europe, it sets event_time in a format like 2024-02-26T07.00.58.787372000Z instead of 2024-02-26T07:00:58.787372000Z, which is not in the ISO 8601 standard format.

As a workaround, users can change their time settings to English, but this creates other problems. I believe I have narrowed down the issue to one line of code specifically.

In Version 2.3.0 of the SDK, a commit was made: "Closes #153. Add additional FCM options for Android Notification #203." This change, in particular, affected the FirebaseAdmin/FirebaseAdmin/Messaging/AndroidNotification.cs file. In this commit, the following code is problematic:

private string EventTimeString
{
    get
    {
        return this.EventTimestamp.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ss.ffffff000'Z'");
    }

    set
    {
        if (string.IsNullOrEmpty(value))
        {
            throw new ArgumentException("Invalid event timestamp. Event timestamp should be a non-empty string");
        }

        this.EventTimestamp = DateTime.Parse(value, CultureInfo.InvariantCulture, DateTimeStyles.None);
    }
}

In the setter method, CultureInfo.InvariantCulture is used, which employs a standard format for parsing the DateTime but is not used in the getter, which is the source of the problem. This omission causes event_time to use the local time settings instead, leading to the incorrect format.

This is a problem because all other versions of Firebase are being deprecated in July 2023. To fix this, the getter should include CultureInfo.InvariantCulture to ensure consistent formatting:

return this.EventTimestamp.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ss.ffffff000'Z'", CultureInfo.InvariantCulture);

Steps to reproduce:

Steps to reproduce: Set up Firebase in a .NET project. Make a message with an AndroidConfig object set up. Set computer settings to use the Finnish format for the Region. (In Windows, this can be done from Clock and Region in the Control Panel.) Set a breakpoint for the response and send the message with either FirebaseMessaging.DefaultInstance.SendEachAsync or FirebaseMessaging.DefaultInstance.SendAsync. Inspect the response in the debugger for the failure message.

Relevant Code:

  var messages = new List<Message>()
            {
                new Message()
                {
                    Topic = EnglishNewsTopic,
                    Android = new AndroidConfig
                    {
                        Notification = new AndroidNotification
                        {
                            Title = EnglishNewsTitle,
                            Body = EnglishNewsBody,
                            ClickAction = "messages"
                        }
                    },

Response Code set Breakpoint at return result

 var result = await FirebaseMessaging.DefaultInstance.SendEachAsync(messages);
                return result;

furofo avatar Feb 26 '24 11:02 furofo

I found a few problems with this issue:

  • I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
  • This issue does not seem to follow the issue template. Make sure you provide all the required information.

google-oss-bot avatar Feb 26 '24 11:02 google-oss-bot

Any updates to this issue/bug @lahirumaramba? There is no way for us to fix this issue without changing the whole timezone settings for our computer/server and that will affect alot of other functionality.

EsposBjorne avatar Apr 25 '24 07:04 EsposBjorne

Yes, only older version works..

2.2.0 works fine.

TMariapori avatar Apr 30 '24 07:04 TMariapori

Yeah!

But what should be do about the deprecation of legacy FCM? SendAll is not usable after 21th of June 2024 and SendEach doesn't work cause of the bug in versions 2.3.0 and above? image

EsposBjorne avatar Apr 30 '24 08:04 EsposBjorne

I have solution for this issue,

before send switch CultureInfo.CurrentCulture to InvariantCulture and after this set back to normal CurrentCulture what you save to variable.

            var oldCulture = CultureInfo.CurrentCulture;
            CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
            // your logic here e.g. await _messaging.SendAsync(..)...
            CultureInfo.CurrentCulture = oldCulture;

now you can send notifications successfully.

TMariapori avatar Apr 30 '24 09:04 TMariapori

@furofo Thanks for highlighting this issue

@TMariapori Will the temporary Culuture change affect the current thread or the whole api in the time that the messages are send ?

Note that this issue was foundallmost two years ago

THere is an open pull request which needs reviewers. https://github.com/firebase/firebase-admin-dotnet/pull/329

larsbloch avatar May 06 '24 12:05 larsbloch

Yes, but this is solution if not wanna fork firebaseadmin. And yes only current thread.

This is correct fix for this bug, but need apply this to this nuget. https://github.com/firebase/firebase-admin-dotnet/pull/329

TMariapori avatar May 06 '24 12:05 TMariapori