Azure Notification Hub REST: Getting error "Failed to parse RegistrationDescription Expected text/cdata" on iOS template registration

Lakshmi Subramanya 10 Reputation points
2023-05-20T20:58:21.96+00:00

Our iOS create registration was working fine till last week and is failing now with the following error:

<Error xmlns:i="http://www.w3.org/2001/XMLSchema-instance">__<Code>400</Code><Detail>Failed to parse RegistrationDescription Expected text/cdata.__TrackingId:4e7f1f5b-7a17-4b92-a204-892847751234,TimeStamp:5/16/2023 9:51:44 AM +00:00</Detail></Error>

So now we have a broken app that can no longer register to notification hub.

Is this something that changed on the hub side or Can we proceed with the workaround with no further issues on registration and notifications?

Note as a workaround, we are able to successfully create the registration through REST.

When we remove CDATA syntax, the registration to the notification hub succeeds.

Workaround:

<BodyTemplate>\n                {\"aps\":{\"alert\":{\"title-loc-key\":\"$(notification_title)\",\"loc-key\":\"$(notification_message)\"}},\"sound\":\"default\",\"content-available\":\"1\",\"message_owner\":\"$(message_owner)\", \"token\":\"$(token)\", \"user\":\"$(user)\"}\n            </BodyTemplate>\n 

iOS Template is same as the template provided in Azure documentation:

    NSString *XML_RegStr = @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<entry xmlns=\"http://www.w3.org/2005/Atom\">\n    <content type=\"application/xml\">\n        <AppleTemplateRegistrationDescription xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.microsoft.com/netservices/2010/10/servicebus/connect\">\n            <Tags>%@</Tags>\n            <DeviceToken>%@</DeviceToken>\n            <BodyTemplate>\n                <![CDATA[{\"aps\":{\"alert\":{\"title-loc-key\":\"$(notification_title)\",\"loc-key\":\"$(notification_message)\"}},\"sound\":\"default\",\"content-available\":\"1\",\"message_owner\":\"$(message_owner)\", \"token\":\"$(token)\", \"user\":\"$(user)\"}]]>\n            </BodyTemplate>\n        </AppleTemplateRegistrationDescription>\n    </content>\n</entry>";

Azure Notification Hubs
Azure Notification Hubs
An Azure service that is used to send push notifications to all major platforms from the cloud or on-premises environments.
295 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Sedat SALMAN 13,265 Reputation points
    2023-05-22T07:06:41.3033333+00:00

    he error message you're encountering, "Failed to parse RegistrationDescription Expected text/cdata," suggests that there is an issue with the CDATA syntax in your iOS template registration for Azure Notification Hub. The CDATA section is used to enclose text that contains special characters, ensuring that they are not treated as markup by the parser. In your case, it seems that the CDATA section is causing a problem during parsing.

    To proceed with a workaround, you have already identified that removing the CDATA syntax allows the registration to succeed. However, please note that removing the CDATA section means that any special characters within the template will not be handled correctly. So if your template contains any special characters, removing the CDATA section might result in unexpected behavior or incorrect rendering of the notification content.

    To address the issue and still preserve the CDATA section, you can try the following steps:

    1. Check the XML encoding: Ensure that the XML encoding specified in the Content-Type header and the XML declaration (<?xml version="1.0" encoding="utf-8"?>) matches. In your case, the encoding is set to UTF-8, so verify that the header and the declaration both specify the same encoding.

    Validate the XML structure: Ensure that the XML structure of your template registration is correct. Verify that all elements and tags are properly nested and closed.

    Escape special characters: If your template contains special characters, make sure they are properly escaped within the CDATA section. For example, characters like <, >, &, or quotes (") should be replaced with their corresponding XML entities (<, >, &, ").

    Test with a minimal template: Try simplifying your template to the minimum required for registration, and gradually add complexity to identify if a specific part of the template is causing the issue. This will help isolate the problem and identify any specific characters or syntax that might be causing the parsing error.

    Validate the Azure Notification Hub configuration: Double-check your Notification Hub configuration to ensure there haven't been any recent changes that could affect the parsing of the registration template. Review the template requirements and ensure that your configuration aligns with the specified guidelines.


  2. Lakshmi Subramanya 10 Reputation points
    2024-03-15T10:52:53.15+00:00

    Solution: The space between the "<BodyTemplate>" and "<![CDATA[>" format was leading to the issue 400 - Bad request.

    Error:

    <BodyTemplate>
            <![CDATA[{Template for the body}]]>
    </BodyTemplate>
    

    On removing the space and sending the request with no new line character and tab space, the request was successful.

    Solution:

    <BodyTemplate><![CDATA[{Template for the body}]]></BodyTemplate>
    
    0 comments No comments