What is the payload for register an iOS device?

Enrico Rossini 176 Reputation points
2024-02-26T23:59:31.1266667+00:00

In my .NET8 MAUI i Want to add the push notification via Azure Notification Hub. Reading the documentation, I wrote this code to create an installation in the hub:

[Export("application:didRegisterForRemoteNotificationsWithDeviceToken:")]
public async void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
    string token = deviceToken.GetBase64EncodedString(NSDataBase64EncodingOptions.None)

    var deviceInstallation = new
    {
        InstallationId = token,
        Platform = "apns"
    };

    using var httpClient = new HttpClient();
    httpClient.DefaultRequestHeaders.Add("x-ms-version", "2015-01");
    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization",
      CreateToken($"https://languageinuse.servicebus.windows.net",
          "DefaultListenSharedAccessSignature", Constants.PushNotificationSecret));

    var r = await httpClient.PutAsJsonAsync($"https://languageinuse.servicebus.windows.net/" +
            $"app/installations/{deviceInstallation.InstallationId}?api-version=2015-01",
                                deviceInstallation);
}

The result is a `Bad request and this is the result

{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.NSUrlSessionHandler+NSUrlSessionDataTaskStreamContent, Headers:
{
  Strict-Transport-Security: max-age=2592000
  trackingid: 979df88e-a15e-4215-8f76-d77c9d23e0b3
  x-ms-correlation-request-id: d1ca2a1e-5b1a-499c-86cf-f9f3f61bb6f1
  Server: Kestrel
  Date: Mon, 26 Feb 2024 23:43:20 GMT
  Strict-Transport-Security: max-age=2592000
  trackingid: 979df88e-a15e-4215-8f76-d77c9d23e0b3
  Content-Type: application/xml
  x-ms-correlation-request-id: d1ca2a1e-5b1a-499c-86cf-f9f3f61bb6f1
}}

I don't understand where the problem is. Is the PushChannel required in the deviceInstallation? If yes, how can I get one? Is the token correct in this request?

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.
263 questions
{count} votes

Accepted answer
  1. SnehaAgrawal-MSFT 18,366 Reputation points
    2024-03-11T07:37:42.01+00:00

    @Enrico Rossini Glad that you were able to resolve your issue and I appreciate your effort in sharing the solution. Your contribution will undoubtedly assist others facing similar challenges.

    As the Microsoft Q&A community follows a policy where the question author cannot accept their own answer

    I've reposted your solution.  Feel free to consider "Accepting" the answer if you find it suitable.

    Problem: Trying to integrate push notifications via Azure Notification Hub but encountering an issue. After consulting the documentation, receiving a 'Bad Request' error message.

    Solution: Step by step details explained in this useful blog- MAUI Push Notifications using Azure Notification Hub for iOS-

    Thanks again for your contribution!

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Enrico Rossini 176 Reputation points
    2024-03-03T12:59:53.4933333+00:00

    Finally, I found the solution and I wrote a post to explain all the steps to send and receive push notification from Azure Notification Hub in iOS

    1 person found this answer helpful.
    0 comments No comments