Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
juniorsaraviao opened this issue Jul 12, 2024 · 10 comments

Comments

@juniorsaraviao
Copy link

juniorsaraviao commented Jul 12, 2024

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

  • Create a .NET Maui app (.NET 8)
  • Follow the steps to create a template registration (link)

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):

  • OS: Mac book Pro - MacOS (Sonoma 14.5)
  • IDE : Visual Studio for Mac 17.6.13
  • Version of the Library used: Microsoft.Azure.NotificationHubs v4.2.0

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

  • [yes] Bug Description Added
  • [yes] Repro Steps Added
  • [yes] Setup information Added
@Hamata6
Copy link

Hamata6 commented Jul 15, 2024

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
Copy link

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
Copy link

alexgavru commented Jul 17, 2024

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
Copy link

Hamata6 commented Jul 22, 2024

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
Copy link
Author

any updates on this issue?

@alexgavru
Copy link

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

@nacho-herrera-appspace
Copy link

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

  • iOS workload version: 17.5.8020
  • Microsoft.Azure.NotificationHubs library version: 4.2.0

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

@alexgavru
Copy link

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
Copy link

Toine-db commented Oct 2, 2024

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

  • XCode 16.0
  • Microsoft.Azure.NotificationHubs 4.2.0

@Toine-db
Copy link

Toine-db commented Oct 3, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants