Search code examples
c#xamarin.iosapple-push-notificationsazure-mobile-servicesazure-notificationhub

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:

  • Xamarin.iOS solution using C#
  • Using PCLCrypto library for encryption
  • I am attempting to recreate the solution demonstrated in an Example in Azure examples github repo but using Xamarin.iOS
  • The connection string is taken directly from Azure portal
  • The code for generating SaS token is adapted from Azure NotificationHub REST Api Documentation
  • Notification hub works, I am able to send a test push notification through the hub UI and i see it come in on the device

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"));

Solution

  • 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")