Azure / azure-notificationhubs-dotnet

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

[BUG] CreateAppleTemplateRegistrationAsync throws an exception on .NET Maui (.NET 8) #328

Open juniorsaraviao opened 1 month ago

juniorsaraviao commented 1 month ago

Describe the bug I'm currently migration a .NET Maui app from .NET 7 to .NET 8. I'm currently using Microsoft.Azure.NotificationHubs v4.2.0 but the app crashes while using CreateAppleTemplateRegistrationAsync

Exception or Stack Trace

[17:22:34 ERR] Error in line 1 position 1029. 'Element' 'DeviceToken' from namespace 'http://schemas.microsoft.com/netservices/2010/10/servicebus/connect' is not expected. Expecting element 'PushVariables'.

To Reproduce

Code Snippet

string APNTemplateBody = "{\"aps\":{\"alert\":{\"title\":\"$(title)\",\"body\":\"$(message)\"}}}";

if (string.IsNullOrEmpty(DeviceTokenForiOS))
{
    return;
}

var registrationId = Preferences.Get(registration_id, string.Empty);
if (!string.IsNullOrEmpty(registrationId))
{
    await hub.DeleteRegistrationAsync(registrationId);
}

var tags = new string[] { tag + userId };
var newRegistration = await hub.CreateAppleTemplateRegistrationAsync(DeviceTokenForiOS, EnvironmentConstants.APNTemplateBody, tags);
Preferences.Set(registration_id, newRegistration.RegistrationId);

Expected behavior The CreateAppleTemplateRegistrationAsync should register the template without any issues. Using the same code in .NET Maui (.NET 7) works as expected.

Setup (please complete the following information):

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

Hamata6 commented 1 month ago

I have the same issue. It suddenly happened. One week ago, my code worked, and last Thursday/Friday I did not change any code and I get this error message. It has something to do with the response that the package receives from Azure, because the registration (in my case) IS made. So the call is successful, but the NuGet Package can't parse the response (I think). EDIT: Can't guarantee my above statement is true, may be an already existing registration.

@TAbbott117 I saw you replying on an other issue, so I tag you, because now the whole package is not working @aimankhan I found you as a contributor specialized in Notification Hub, so tagged you too.

BTW, the calls for Android stuff are working fine, no exceptions on those method calls. Only the Apple calls are returning this exceptions

alexgavru commented 1 month ago

I have the exactly the same issue, it started happening after the pipeline workloads automatically updated some sdks (with red are the new values): image (8)

alexgavru commented 1 month ago

I found a shady workaround for this issue. I integrated the NotificationHubs project directly into my solution and modified the AppleRegistrationDescription at runtime. I added the PushVariables element before the DeviceToken element:

image

Steps:

  1. Integrate this repo directly into your project and remove the nuget dependencies.
  2. Update all the nuggets in the project to latest versions
  3. In NotificationHubClient.cs in methods ReadEntitiesAsync(Stream source) and ReadEntityAsync(Stream source). Basically read the stream, parse the xml and detect if PushVariables is not there and add it, then pass a new stream based on the modification you just made.

I didn't investigate to see why the error is there, at first sight it seems that something is not working right in the System.Serialization or XMLSerializer, because it doesn't take into account that PushVariables element is optional/not required. Hence the fix to add an empty PushVariables element if it doesn't exist.

Hamata6 commented 1 month ago

It doesn't seem like we are getting a response here. I just checked and it still crashes. What to do? Submit the bug elsewhere? But where? @TAbbott117 @aimankhan @pmngo @jessHuh

juniorsaraviao commented 3 weeks ago

any updates on this issue?