Forum Xamarin.iOS

Xamarin.iOS Bad request when registering Push Notification template with Azure Notification Hub

Cdn_EuroCdn_Euro Member ✭✭✭
edited September 2019 in Xamarin.iOS

I do not know why I am getting this error, this code worked 7 months ago when I last worked on it. I have a web api associated with notification hub and this is how I register from within the iOS project of the Xamarin project

In AppDelegate.cs:

private async Task SendRegistrationToServerAsync(NSData deviceToken)
        {
            const string templateBodyAPNS = @"{
                                                ""aps"" : {
                                                    ""alert"" : ""$(messageParam)"",
                                                    ""mutable-content"": 1
                                                },
                                            }";


            var templates = new JObject();
            templates["genericMessage"] = new JObject
            {
                {"body", templateBodyAPNS }
            };

            // send registration to web api for DEVELOPMENT
            var client = new MobileServiceClient(AppConstants.AZURE_DEVELOPMENT_WEB_API_URL);

            // here the AppConstants.AZURE_DEVELOPMENT_WEB_API_URL looks like this:
            // https://myappservicename.azurewebsites.net"

            try
            {   
                await client.GetPush().RegisterAsync(deviceToken, templates);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);           
            }

        }

I get a "Bad Request" in the catch block, any idea why this could be please?

Best Answer

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai
    Accepted Answer

    Firstly, make sure your application has connected to the internet.
    Then check your push notification certificate's expiration.
    Try to update the Microsoft.Azure.Mobile.Client to the latest version.
    If the issue persists you can try to use Xamarin.Azure.NotificationHubs.iOS to register the template following this documentation:
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/azure-services/azure-notification-hub#register-with-azure-notification-hub-and-subscribe-to-tags

  • Cdn_EuroCdn_Euro Member ✭✭✭
    edited September 2019

    @LandLu I used the documentation (seems things changed a little since last working on this app) and push notifications work perfectly with my iPhone 5S test device with iOS 12.4.1

    BUT, when testing on my iPhone 8 Plus with latest iOS 13 version I get this error (also should specify this device also was registered for push notifications in production mode as well, maybe thats why I cant use it for development testing now and getting this error, maybe some mix up with production-development push notification registrations with the azure?):

    =================================================================
        Native Crash Reporting
    =================================================================
    Got a SIGSEGV while executing native code. This usually indicates
    a fatal error in the mono runtime or one of the native libraries 
    used by your application.
    =================================================================
    
    =================================================================
        Native stacktrace:
    =================================================================
        0x103ef09c8 - /private/var/containers/Bundle/Application/10179C87-9D6C-4E2B-90AD- 
        57BF86C9C934/myapp.iOS.app/myapp.iOS : (null)
        0x103ee67b0 - /private/var/containers/Bundle/Application/10179C87-9D6C-4E2B-90AD- 
        57BF86C9C934/myapp.iOS.app/myapp.iOS : (null)
        0x103ef4ff0 - /private/var/containers/Bundle/Application/10179C87-9D6C-4E2B-90AD- 
        57BF86C9C934/myapp.iOS.app/myapp.iOS : mono_pmip
        0x195cdf894 - /usr/lib/system/libsystem_platform.dylib : <redacted>
        0x10214029c - /private/var/containers/Bundle/Applica
        tion/10179C87-9D6C-4E2B-90AD-57BF86C9C934/ShakeAlarmRR1.iOS.app/ShakeAlarmRR1.iOS : (null)
        0x1021403fc - /private/var/containers/Bundle/Application/10179C87-9D6C-4E2B-90AD- 
        57BF86C9C934/myapp.iOS.app/myapp.iOS : (null)
        0x102147ff8 - /private/var/containers/Bundle/Application/10179C87-9D6C-4E2B-90AD- 
        57BF86C9C934/myapp.iOS.app/myapp.iOS : (null)
        0x102147b38 - /private/var/containers/Bundle/Application/10179C87-9D6C-4E2B-90AD- 
        57BF86C9C934/myapp.iOS.app/myapp.iOS : (null)
        0x102146364 - /private/var/containers/Bundle/Application/10179C87-9D6C-4E2B-90AD- 
        57BF86C9C934/myapp.iOS.app/myapp.iOS : (null)
        0x102145768 - /private/var/containers/Bundle/Application/10179C87-9D6C-4E2B-90AD- 
        57BF86C9C934/myapp.iOS.app/myapp.iOS : (null)
        0x102144620 - /private/var/containers/Bundle/Application/10179C87-9D6C-4E2B-90AD- 
        57BF86C9C934/myapp.iOS.app/myapp.iOS : (null)
        0x1963b90b0 - /System/Library/Frameworks/Foundation.framework/Foundation : <redacted>
        0x195ced1ec - /usr/lib/system/libsystem_pthread.dylib : _pthread_start
        0x195cf0aec - /usr/lib/system/libsystem_pthread.dylib : thread_start
    
    =================================================================
        Basic Fault Address Reporting
    =================================================================
    Memory around native instruction pointer (0x195cdef44):0x195cdef34  c0 03 5f d6 1f 20 03 d5 1f 20 03 
    d5 01 ec 7c 92  .._.. ... ....|.
    0x195cdef44  20 00 c0 3d c3 f9 ff 10 62 04 c1 3c 02 0c 40 92   ..
    =....b..<[email protected]
    0x195cdef54  63 00 02 cb 61 00 c0 3d 00 1c a1 4e 05 00 00 14  c...a..=...N....
    0x195cdef64  1f 20 03 d5 1f 20 03 d5 1f 20 03 d5 20 0c c1 3c  . ... ... .. ..<
    

    And in AppDelegate.cs this is the updated method for push notification registration following the documentation, and this is the point at which the error triggers:

    public override void RegisteredForRemoteNotifications(
                UIApplication application, NSData deviceToken)
            {
                AzureNotifHub = new SBNotificationHub(AppConstants.AZURE_DEVNOTIFHUB3_LISTENSHARED, 
      AppConstants.DEV_NOTIFHUB3);
    
                // update registration with Azure Notification Hub
                AzureNotifHub.UnregisterAllAsync(deviceToken, (error) =>
                {
                    if (error != null)
                    {
                        Debug.WriteLine($"Unable to call unregister {error}");
                        return;
                    }
    
                    var tags = new NSSet(AppConstants.IOSSUBSCRIPTIONTAGS.ToArray());
                    AzureNotifHub.RegisterNativeAsync(deviceToken, tags, (errorCallback) =>
                    {
                        if (errorCallback != null)
                        {
                            Debug.WriteLine($"RegisterNativeAsync error: {errorCallback}");
                        }
                    });
    
                    var templateExpiration = 
     DateTime.Now.AddDays(120).ToString(System.Globalization.CultureInfo.CreateSpecificCulture("en- 
     US"));
                    AzureNotifHub.RegisterTemplateAsync(deviceToken, "defaultTemplate", 
     AppConstants.IOS_APNS_TEMPLATE_BODY, templateExpiration, tags, (errorCallback) =>
                    {
                        if (errorCallback != null)
                        {
                            if (errorCallback != null)
                            {
                                Debug.WriteLine($"RegisterTemplateAsync error: {errorCallback}");
                            }
                        }
                    });
                });
    

    I have 3 development hubs created in Azure, I tried creating news ones to see if the error still occurs on the iPhone 8 Plus, and yes indeed same error.

    @LandLu you are really great with guidance, do you have any ideas why this error might occur (again with same exact code that works perfect on the iPhone 5S test device with iOS 12.4.1)?

  • DLBrandtDLBrandt Member

    @Cdn_Euro I am having the same issues with iOS 12 working, but not iOS 13. Did you find a solution? Thanks!

  • imagineblueeyesimagineblueeyes USUniversity ✭✭

    Hey Cdn_Euro, whatever happened with this? I am in the same spot.

    Can you provide code samples of how to cure this?

    Thanks...

  • Menno.5004Menno.5004 USMember ✭✭
    edited April 25

    I found out the hard way, you are limited in what characters can be used in "tags":

    From the documentation (can't post links yet):
    A tag can be any string, up to 120 characters, containing alphanumeric and the following non-alphanumeric characters: ‘_’, ‘@’, ‘#’, ‘.’, ‘:’, ‘-’.

    Also, the tag cannot be empty as it seems...

    Example:
    `public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken) {
    var Hub = new SBNotificationHub("connectionString", "hubName");

                Hub.UnregisterAll(deviceToken, (error) =>
                {
                    if (error != null)
                    {
                        Debug.WriteLine($"Unable to call unregister {error}");
                        return;
                    }
    
                    var tags = new NSSet("default");
                    Hub.RegisterNative(deviceToken, tags, (errorCallback) =>
                    {
                        if (errorCallback != null)
                        {
                            Debug.WriteLine($"RegisterNativeAsync error: {errorCallback}");
                        }
                    });
    
                    var templateExpiration = DateTime.Now.AddDays(120).ToString(System.Globalization.CultureInfo.CreateSpecificCulture("en-US"));
                    Hub.RegisterTemplate(deviceToken, "defaultTemplate", Constants.APNTemplateBody, templateExpiration, tags, (errorCallback) =>
                    {
                        if (errorCallback != null)
                        {
                            if (errorCallback != null)
                            {
                                Debug.WriteLine($"RegisterTemplateAsync error: {errorCallback}");
                            }
                        }
                    });
    
                });
    

    }`

Sign In or Register to comment.