App Crashes while registering with Notification Hub under Release build, but not Debug

AARON PRICE BEHUNIN 10 Reputation points
2023-12-01T20:09:41.9133333+00:00

I created an app that uses Azure Push Notifications. I've been able to get it to run on a physical iPad without any problems under a debug build. But when I try to implement it under a release build, it crashes.

My debugging skills indicate that the app crashes when running '_hubClient.GetRegistrationsByChannelAsync()'.

Notification Hub is using Token based authentication in Production mode.

Looking at the iPad's console via xcode, it throws this error:

Unhandled managed exception: ErrorInLine, 1, 1759 UnexpectedElementExpectingElements, Element, DeviceToken, http://schemas.microsoft.com/netservices/2010/10/servicebus/connect, Tags (System.Runtime.Serialization.SerializationException)
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.ThrowRequiredMemberMissingException(XmlReaderDelegator , Int32 , Int32 , XmlDictionaryString[] )
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.GetMemberIndexWithRequiredMembers(XmlReaderDelegator , XmlDictionaryString[] , XmlDictionaryString[] , Int32 , Int32 , ExtensionDataObject )
   at System.Runtime.Serialization.ReflectionXmlReader.ReflectionReadMembers(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString[] , XmlDictionaryString[] , ClassDataContract , Object& )
   at System.Runtime.Serialization.ReflectionReader.ReflectionReadClass(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString[] , XmlDictionaryString[] , ClassDataContract )
   at <…>

Here is my code:

[Export("application:didRegisterForRemoteNotificationsWithDeviceToken:")]
    public async void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
    {

        byte[] token = deviceToken.ToArray();

        Constants.notificationToken = token;
        var _hubClient = 	   NotificationHubClient.CreateClientFromConnectionString(Constants.NotificationIP, Constants.NotificationHub);
        //this is notification hub client  

        var registration = new AppleRegistrationDescription(deviceToken.DebugDescription.Trim('<', '>').Replace(" ", string.Empty).ToUpperInvariant());


        var regs = await _hubClient.GetRegistrationsByChannelAsync(registration.PnsHandle, 1);

        if (regs.Count() == 0)
            await _hubClient.CreateRegistrationAsync(registration);


        var _notificationInstance = new INotifications();

        _notificationInstance.RegisterForNotifications("add");

    }
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.
288 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,561 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,806 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,116 questions
{count} vote

2 answers

Sort by: Most helpful
  1. brtrach-MSFT 15,701 Reputation points Microsoft Employee
    2023-12-07T04:48:57.7066667+00:00

    @AARON PRICE BEHUNIN It seems that your app is crashing when running _hubClient.GetRegistrationsByChannelAsync() under a release build. However, it works fine under a debug build. The error message you provided indicates that there is an issue with the serialization of the device token.

    One possible reason for this issue is that the device token format has changed in Xcode 11, which could cause existing code to break. You may need to update the Azure Notification Hubs SDK to version 2.0.4 or newer to accommodate this change.

    Another possible reason is that the device token is not being trimmed correctly. You can try modifying the following line of code:

    var registration = new AppleRegistrationDescription(deviceToken.DebugDescription.Trim('<', '>').Replace(" ", string.Empty).ToUpperInvariant());
    

    to:

    var registration = new AppleRegistrationDescription(deviceToken.Description.Trim('<', '>').Replace(" ", string.Empty).ToUpperInvariant());
    

    This should ensure that the device token is trimmed correctly.

    Finally, wrapping the code in a try/catch block is a good way to avoid the crash, but it is important to investigate the root cause of the issue to ensure that it does not cause any other problems in the future.


  2. brtrach-MSFT 15,701 Reputation points Microsoft Employee
    2023-12-12T05:22:47.87+00:00

    @AARON PRICE BEHUNIN I'm sorry to hear that the suggested solutions did not work. It seems that the issue is not related to the device token format or the version of the Azure Notification Hubs SDK.

    The error message you provided indicates that there is an issue with the serialization of the device token. One possible reason for this issue is that the device token is not being converted to a byte array correctly. You can try modifying the following line of code:

    byte[] token = deviceToken.ToArray();
    

    to:

    byte[] token = new byte[deviceToken.Length];
    Marshal.Copy(deviceToken.Bytes, token, 0, (int)deviceToken.Length);
    

    This should ensure that the device token is converted to a byte array correctly.

    If you still receive an error after attempting the above, please let us know as we have one final item to try but this is step 2 in understanding the error. We look forward to your reply.