Azure / azure-notificationhubs-dotnet

.NET SDK for Azure Notification Hubs
MIT License
70 stars 122 forks source link

Unable to set priority for FCM templated messages #176

Closed stpatrick2016 closed 3 years ago

stpatrick2016 commented 3 years ago

Describe the bug In order to set priority for FCM messages, one needs to declare the priority tag on the same level as the data tag or, in the latest version it should be under the android tag. When using FcmTemplateRegistrationDescription with such template, an exception System.Xml.XmlException: There are multiple root elements is raised though.

Exception or Stack Trace

System.AggregateException: One or more errors occurred. (There are multiple root elements.)
 ---> System.Xml.XmlException: There are multiple root elements.
   at System.Xml.XmlExceptionHelper.ThrowXmlException(XmlDictionaryReader reader, String res, String arg1, String arg2, String arg3)
   at System.Xml.XmlBaseReader.EnterScope()
   at System.Runtime.Serialization.Json.XmlJsonReader.ReadNonExistentElementName(StringHandleConstStringType elementName)
   at System.Runtime.Serialization.Json.XmlJsonReader.Read()
   at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r)
   at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r, LoadOptions o)
   at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)
   at Microsoft.Azure.NotificationHubs.FcmTemplateRegistrationDescription.OnValidate()
   at Microsoft.Azure.NotificationHubs.RegistrationDescription.Validate(Boolean checkExpirationTime)
   at Microsoft.Azure.NotificationHubs.RegistrationSDKHelper.ValidateRegistration(RegistrationDescription registration)
   at Microsoft.Azure.NotificationHubs.NotificationHubClient.CreateOrUpdateRegistrationImplAsync[TRegistration](TRegistration registration, EntityOperatonType operationType, CancellationToken cancellationToken)

To Reproduce Steps to reproduce the behavior:

  1. Create FcmTemplateRegistrationDescription with template like this:
    {
    "data":{
        "message":"$(message)",
    },
    "priority":"high"
    }
  2. Call CreateOrUpdateRegistrationAsync

Code Snippet The code snippet that causes the issue.

var hub = NotificationHubClient.CreateClientFromConnectionString("put your connection string here", "your hub name");
var registration = new FcmTemplateRegistrationDescription(token, @"
            {
                ""data"":{
                    ""message"":""$(message)"",
                },
                ""priority"":""high""
            }");
registration.RegistrationId = await hub.CreateRegistrationIdAsync();
await hub.CreateOrUpdateRegistrationAsync(registration); //will raise exception

Expected behavior The template should be accepted, or there should be another option to set properties such as priority (or any other of the dozens of properties)

Setup (please complete the following information):

Additional context I believe the problem lies in the FcmTemplateRegistrationDescription.OnValidate, line 294, which explicitly requires to have only 1 root.

Information Checklist Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report

stpatrick2016 commented 3 years ago

Actually looks like I have found the problem - the error returned by XmlReader is misleading. The problem was in the extra comma after the message tag. Removing it it has worked as expected. Took me half day to figure out and only saw that after re-reading this post. Closing the issue - misleading error is not relevant to this library anyway.