Dmitry Samuylov
Dmitry Samuylov

Reputation: 1564

Send Push Notification from a device via Azure NotificationHub REST Api

I am trying to send a push notification from an iOS device (iPhone) via Azure NotificationHub REST Api. I am attempting this from a Xamarin.iOS solution following the Azure documentation I found online.

Response returns following info:

Error: '50002: Provider Internal Error' Status code: 500

Code used to invoke NotificationHub REST Api (from iOS client app):

var hubUtil = new NotificationHubUtility("Endpoint=sb://company-name.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=00000000000000011111111111111122222222222222");
hubUtil.SendNotificationMessage("This is a TEST Notification!").ConfigureAwait(false);

public class NotificationHubUtility
{
    public string Endpoint { get; private set; }
    public string SasKeyName { get; private set; }
    public string SasKeyValue { get; private set; }
    public string HubName { get; private set; }
    public string ApiVersion { get; private set; }

    public NotificationHubUtility(string connectionString)
    {
        //Parse Connectionstring
        string[] parts = connectionString.Split(new char[] {';'});
        for (int i = 0; i < parts.Length; i++)
        {
            if (parts[i].StartsWith("Endpoint", StringComparison.CurrentCulture))
                Endpoint = "https" + parts[i].Substring(11);
            if (parts[i].StartsWith("SharedAccessKeyName", StringComparison.CurrentCulture))
                SasKeyName = parts[i].Substring(20);
            if (parts[i].StartsWith("SharedAccessKey", StringComparison.CurrentCulture))
                SasKeyValue = parts[i].Substring(16);
        }

        HubName = "my-hub";
        ApiVersion = "?api-version=2014-09-01";
    }

    public string GetSaSToken(string uri, int minUntilExpire)
    {
        string targetUri = Uri.EscapeDataString(uri.ToLower()).ToLower();

        // Add an expiration in seconds to it.
        long expiresOnDate = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
        expiresOnDate += minUntilExpire * 60 * 1000;
        long expires_seconds = expiresOnDate / 1000;
        var toSign = targetUri + "\n" + expires_seconds;

        // Generate a HMAC-SHA256 hash or the uri and expiration using your secret key.
        IMacAlgorithmProvider hasher = WinRTCrypto.MacAlgorithmProvider.OpenAlgorithm(MacAlgorithm.HmacSha256);
        var messageBuffer = WinRTCrypto.CryptographicBuffer.ConvertStringToBinary(toSign, Encoding.UTF8);
        var keyBuffer = WinRTCrypto.CryptographicBuffer.ConvertStringToBinary(SasKeyValue, Encoding.UTF8);
        var hmacKey = hasher.CreateKey(keyBuffer);
        var signedMessage = WinRTCrypto.CryptographicEngine.Sign(hmacKey, messageBuffer);

        string signature = Uri.EscapeDataString(WinRTCrypto.CryptographicBuffer.EncodeToBase64String(signedMessage));

        var token = "SharedAccessSignature sig=" + signature + "&se=" + expires_seconds + "&skn=" + SasKeyName + "&sr=" + targetUri;

        return token;
    }

    public async Task SendNotificationMessage(string message)
    {
        try
        {
            // basic http client (if needed)
            var httpClient = new HttpClient();
            httpClient.MaxResponseContentBufferSize = 1024000;

            var notificationPayload = "{\"aps\":{\"alert\":\"" + message + "\"}}";
            var notificationHubUrl = $"{Endpoint}{HubName}/messages/{ApiVersion}";
            var authToken = GetSaSToken(notificationHubUrl, 10);

            var request = new HttpRequestMessage(HttpMethod.Post, notificationHubUrl);
            //request.Headers.Add("Content-Type", "application/json;charset=utf-8");
            request.Headers.Add("ServiceBusNotification-Format", "apple");
            request.Headers.Add("ServiceBusNotification-Apns-Expiry", DateTime.UtcNow.AddYears(1).ToString("YYYY-MM-DDThh:mmTZD"));
            request.Headers.Add("Authorization", authToken);
            var requestBody = new StringContent(notificationPayload, Encoding.UTF8, "application/json");
            request.Content = requestBody;

            var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead);

        }
        catch (Exception ex)
        {
            Console.Error.WriteLine(@"ERROR - Sending Notification {0}", ex.Message);
        }
    }
}

Example of Connection String:

Endpoint=sb://company-name.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=00000000000000011111111111111122222222222222

Environment and Assumptions:

What am I missing here? I wasn't able to find much relevant documentation for this error online. Any help would be greatly appreciated.

Update with Fix:

The following 2 changes to the code above fixed the issue for me:

Changed

ApiVersion = "?api-version=2014-09-01";

to

ApiVersion = "?api-version=2016-07";

Changed

request.Headers.Add("ServiceBusNotification-Apns-Expiry", DateTime.UtcNow.AddYears(1).ToString("YYYY-MM-DDThh:mmTZD"));

to

request.Headers.Add("ServiceBusNotification-Apns-Expiry", DateTime.UtcNow.AddYears(1).ToString("yyyy-MM-ddTHH:mm:sszzz"));

Upvotes: 2

Views: 747

Answers (2)

Dmitry Samuylov
Dmitry Samuylov

Reputation: 1564

I've figured out the issue(s):

  1. +1 to Sohrab for pointing out the Api Version, I updated it to ApiVersion = "?api-version=2016-07";
  2. There was an error in the ServiceBusNotification-Apns-Expiry header value format, the date was not being correctly formatted to string. Corrected format string is this ToString("yyyy-MM-ddTHH:mm:sszzz")

Upvotes: 0

Sohrab
Sohrab

Reputation: 21

The Api Version 2014-09-01 is not correct. Please use 2016-07 as the Api version and you should be good.

Thanks

Sohrab

Upvotes: 1

Related Questions