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

Sending notifications using C# FirebaseAdmin 2.4.0 doesn't work. Invalid value at 'message.android.notification.event_time' #370

Closed EsposBjorne closed 3 weeks ago

EsposBjorne commented 10 months ago

Sending notifications using C# with Nuget package "FirebaseAdmin" version 2.4.0 doesn't work. ERROR: Invalid value at 'message.android.notification.event_time'.

Our currently working FirebaseAdmin version is 1.1.4 and that is using the legacy cloud messaging API. Because of the migration to the new API which need to happend before june 2024, we updated to the latest FirebaseAdmin 2.4.0 that uses the new api with versioning "/V1". We earlier used "SendAllAsync", but are now trying to use "SendEachAsync" do to deprecation.

firebase

Our current AndroidConfig looks like this:

Android = new AndroidConfig
{
    Notification = new AndroidNotification
    {
        ClickAction = "FLUTTER_NOTIFICATION_CLICK",
        ChannelId = "fcm_default_channel",
        Icon = "ic_notif",
        EventTimestamp = DateTime.UtcNow //Added afterwards cause of error but didn't resolve our issue.
    },
    CollapseKey = "type_a"
},

Somehow the EventTimestamp is not working properly. Any suggestions?

EsposBjorne commented 9 months ago

Anyone?

furofo commented 7 months ago

Actually, I believe I found the reason for this issue. It has to do with the time settings on your computer. One workaround is to change the time settings to English because, 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.

In Version 2.3.0 of the SDK, a commit was made: "Closes #153. Add additional FCM options for Android Notification #203." This changed in particular 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. 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);
larsbloch commented 5 months ago

This is actually an open pull request.

https://github.com/firebase/firebase-admin-dotnet/pull/329

I have even tried reaching out to google support to get some action on this one.

The fix is so simple. I wish a responsible could make sure this fix was pushed out.

klaus-stabil-it commented 1 month ago

To continue using this package I made a local copy of the repo, in file AndroidNotification.cs line 363 I changed [JsonProperty("event_time")] to [JsonIgnore] And now I can send messages again. Of course this is not the right way to fix the problem, but nothing has been done since december 23...