OneSignal / onesignal-dotnet-api

Other
13 stars 11 forks source link

[Bug]: Cannot use Filter as field always throw null error #21

Open banhawchun opened 1 year ago

banhawchun commented 1 year ago

What happened?

I want to send notification base on user tags filter but it seems like i cannot initialize Filter() without it throws null error:

ArgumentNullException: Value cannot be null. Parameter name: field is a required property for Filter and cannot be null OneSignalApi.Model.Filter..ctor(string field, string key, string value, RelationEnum relation) in Filter.cs, line 116

Steps to reproduce?

public static async Task<string> testSendAsync()
        {
            appConfig.BasePath = "https://onesignal.com/api/v1";
            appConfig.AccessToken = Authorization;
            var appInstance = new DefaultApi(appConfig);

            var notification = new Notification(appId: AppId)
            {
                Contents = new StringMap(en: "Hello World from .NET!"),
                Headings = new StringMap(en: "Hello"),
                IncludedSegments = new List<string> { "Sale Role" },
                Filters = new List<Filter>
                {
                    new Filter(){ Field = "tag", Key = "Role", Value = "4", Relation = Filter.RelationEnum.Equal}
                },
            };

            var response = await appInstance.CreateNotificationAsync(notification);

            return $"Alert success: {response.Recipients.ToString()}";
        }

What did you expect to happen?

It should not be null when we provide value to "field" already

Code of Conduct

bnoffer commented 1 year ago

Looks like the error is due to the constructor performing a null check. If you use the constructor instead of initializing manually it should work fine.

In addition you can not combine segments and Filters in one notification. You can check the API Reference on that: https://documentation.onesignal.com/reference/create-notification#send-to-users-based-on-filters

oldmanhd7 commented 5 months ago

Hello bro, were you able to solve this? I am having the same problem and I have not been able to solve it, I have had to use IncludePlayerIds which is a function that is depreciated, thanks a lot