Xamarin.iOS Azure Notification Hub works in Production but second Hub set to Sandbox does not work

Cdn_EuroCdn_Euro Member ✭✭✭

Goal: I am looking to have a Notification Hub for testing and another for production when publishing the app. Ideally I want to use the Sandbox Notification Hub to target the installation id of my device for testing push notifications.

Note: With Android for testing, I just use the Production Hub to test, and just target the Android installation ID of my device and it works. But iOS Push notifications work with certificates and Sandbox/Production modes.

In Azure I have a Notification Hub Namespace. The Namespace contains two hubs, one set in Production mode, the other set to Sandbox (by flipping the switch)

These are the steps I covered:

  1. The production hub has a production p12 certificate uploaded that was previously exported from the Keychain by right clicking "Apple Push Services: com.company.myappname"

2.The sandbox hub has a sandbox p12 certificate uploaded that was previously exported from the Keychain by right clicking "Apple Development iOS Push Services: com.company.myappname"

  1. There are two Post methods in a web API hosted in Azure for each production and sandbox modes. What differs between the methods is switching the hub and default shared endpoint to their corresponding values in Azure Portal.

        [AllowAnonymous]
        [HttpPost, Route("sendForIOSDevelopment")]
        public async Task<IHttpActionResult> SendTestToIOSInstallationId([FromBody]string message, string installationId)
        {        
    
            if (string.IsNullOrWhiteSpace(installationId))
            {
                var model = new
                {
                    error = new
                    {
                        code = 400,
                        message = "installation id is null or empty"
                    }
                };
                return Content(HttpStatusCode.BadRequest, model); //400 Bad Request with error message
            }
    
            //string hubName = "sandboxModeHub";
            string hubName = "productionModeHub";
    
            //string hubNameDefaultShared = "sandboxModeHubFullSharedEndPoint";
            string hubNameDefaultShared = "productionModeHubFullSharedEndPoint";
    
            NotificationHubClient hub = NotificationHubClient
                            .CreateClientFromConnectionString(hubNameDefaultShared, hubName, enableTestSend: true);
    
            var templateParams = new Dictionary<string, string>
            {
                ["messageParam"] = message
            };
    
            NotificationOutcome result = await hub.SendTemplateNotificationAsync(templateParams, "$InstallationId:{" + installationId + "}").ConfigureAwait(false);
    
            return Ok(result); //200 Ok with model result
        }
    

4.This is how I register for push notifications in the mobile app:

private async Task SendRegistrationToServerAsync(NSData deviceToken)
        {
            //this is the template/payload used by iOS. It contains the "messageParam"
            // that will be replaced by our service

            const string templateBodyAPNS = @"{
                                                ""aps"" : {
                                                    ""alert"" : ""$(messageParam)"",
                                                    ""mutable-content"": 1
                                                },
                                            }";


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

            // send registration to web api
            var client = new MobileServiceClient(MyAppName.App.MobileServiceUrl);
            await client.GetPush().RegisterAsync(deviceToken, templates);

            //get the installation id
            Console.WriteLine("Installation id: " + client.InstallationId.ToString());      
        }
  1. To run the Xamarin app in Sandbox mode, I change the Entitlements.plist "aps-environment" key to "development" and use Postman to target the development Post method hosted in the Azure backend.

    What happens is that everything works fine on the Production hub but not on the Sandbox hub.

When I post to the Sandbox hub using the development route to target the development Post method in the Web API i get this :

{
"Success": 0,
"Failure": 0,
"Results": null
}

I cannot figure out why that is up to this point. I did already use my device in both production mode and sandbox to test the app, wondering if that has anything to do with it.

Answers

  • andreyrcandreyrc Member

    I have the same problem.

    The generated certificate of the type

    Apple Development iOS Push Services: Can not upload p.12 file as SandBox
    And the strangest thing is that selected as Production it returns the message successfully.

    Since the file and the certificate were generated as Development.

    Very strange!

    Managed to solve?

  • andreyrcandreyrc Member

    The hotfix has already been corrected by Microsoft, as follows:

    https : // social.msdn.microsoft.com/Forums/azure/en-US/5eb4c550-e3bf-4f04-ab82-8e153d069d4f/apns-dev-cert-upload-fails-400-bad-request-call-to-sspi-fails-with-sandbox-selected-succeeds-for?forum=notificationhubs

    For me, it worked!

Sign In or Register to comment.