firebase / firebase-admin-dotnet

Firebase Admin .NET SDK
https://firebase.google.com/docs/admin/setup
Apache License 2.0
369 stars 131 forks source link

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

Closed furofo closed 3 weeks ago

furofo commented 7 months ago

[REQUIRED] Step 2: Describe your environment

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;
google-oss-bot commented 7 months ago

I found a few problems with this issue:

EsposBjorne commented 5 months ago

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.

TMariapori commented 5 months ago

Yes, only older version works..

2.2.0 works fine.

EsposBjorne commented 5 months ago

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

TMariapori commented 5 months ago

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.

larsbloch commented 5 months ago

@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

TMariapori commented 5 months ago

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

lahirumaramba commented 1 month ago

Thanks for your patience folks! This is covered in #417 now