Azure / azure-notificationhubs-dotnet

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

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

Open juniorsaraviao opened 4 months ago

juniorsaraviao commented 4 months 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 4 months 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 4 months 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 4 months 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 3 months 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 months ago

any updates on this issue?

alexgavru commented 2 months ago

Good news, with the latest workloads, it started working again. I reverted my workaround.

image
nacho-herrera-appspace commented 2 months ago

Hi! I just tried the latest workload and got the following exception.

System.Runtime.Serialization.SerializationException: ErrorInLine, 1, 1098 UnexpectedElementExpectingElements, Element, DeviceToken, http://schemas.microsoft.com/netservices/2010/10/servicebus/connect, PushVariables

alexgavru commented 2 months ago

I did a complete cleanup of my environment. Deleted dotnet folder, installed 8.0.401 .NET SDK, and ran command: "sudo dotnet workload install maui". Workaround was not needed anymore for me. For now it seems to work, I can let you know later this month once we release this to production if it went well or not. :)

Toine-db commented 1 month ago

Same issue here

RegisteredForRemoteNotifications Exception: System.Runtime.Serialization.SerializationException: Error in line 1 position 1009. 'Element' 'DeviceToken' from namespace 'http://schemas.microsoft.com/netservices/2010/10/servicebus/connect' is not expected. Expecting element 'PushVariables'.

Reinstalled dotnet 8.0.402 (version newer) and did dotnet workload install maui... > resulting in MAUI 8.0.82/8.0.100 Besides that I work with

Toine-db commented 1 month ago

I found a workaround, for my use case....but there are still issue in this repo when 'PushVariables' or 'Tags' aren't used.

var appleRegistration = new AppleRegistrationDescription(token, tags);
appleRegistration.PushVariables = new Dictionary<string, string>()
{
     { "azure_notificationhubs_dotnet_issue", "missing PushVariables fix" }
};
var registration = await hub.CreateRegistrationAsync(appleRegistration);

Currently im always re-creating a registration, instead of retrieving an old one. Because methods like GetRegistrationsByChannelAsync do get the same errors, and by removing old registrations and re-creating new, it won't get the errors.