0
votes

Basically I want to be able to send a push notification for a specific registered device (using a tag) to the Azure Notification Hub which will then send that notification out to the device. I do have some c# code that will do this but I'd like to make use of an API if possible.

I found an article (computer restarted tho so I don't have the link) that explained how to send notifications using an API but it looks like it sends the notification to every device registered with the hub so I want to see if it would be easy to modify the body or even the pre-request script to include a specific device tag.

Or if any of you smart people know of an easier way to do this through an API then I'm all ears.

Here's my code (taken from Postman) that I currently have that's sending the notification to all hub devices.

POST endpoint: https://{namespace_name}.servicebus.windows.net/{hub_name}/messages/?api-version=2015-01

Headers:

  • Authorization - {{azure-authorization}}
  • Content-Type - application/json
  • ServiceBusNotification-Format - gcm

Request body:

{"data":
    {
        "gcm.notification.body":"Hi", 
        "gcm.notification.title":"Hi",
    }
}

Pre-request Script:

function getAuthHeader(resourceUri, keyName, key) {

    var d = new Date();
    var sinceEpoch = Math.round(d.getTime() / 1000);
    var expiry = (sinceEpoch + 3600);
    var stringToSign = encodeURIComponent(resourceUri) + '\n' + expiry;
    var hash = CryptoJS.HmacSHA256(stringToSign, key);
    var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
    var sasToken = 'SharedAccessSignature sr=' + encodeURIComponent(resourceUri) + '&sig=' + encodeURIComponent(hashInBase64) + '&se=' + expiry + '&skn=' + keyName;
    console.log(sasToken);

    return sasToken;
}
postman.setEnvironmentVariable('azure-authorization', getAuthHeader(request.url,"DefaultFullSharedAccessSignature", "<full_shared_access_signature>"));
postman.setEnvironmentVariable('current-date',new Date().toUTCString());
1
Is this document you are looking for? ServiceBusNotification-Tags (optional): Specifies the tag (or tag expression) defining the targeted set of registrations. If this header is not present, the notification hub broadcasts to all registrations.Jack Hua
@JackHua-MSFT that looks to be exactly what i needed. add it as a reply and ill mark it as the answer :)user1818298

1 Answers

1
votes

The REST API for sending notifications is a simple POST on /yourHub/messages, with special headers. When sending notifications in a platform-native format, the body is the platform-specific body to be sent. The additional headers are:

  • ServiceBusNotification-Format: Specifies that the platform (if sending a native notification) or “template” is to send a template notification.
  • ServiceBusNotification-Tags (optional): Specifies the tag (or tag expression) defining the targeted set of registrations. If this header is not present, the notification hub broadcasts to all registrations.

Other headers are supported for platform-specific functionality, as specified in the Notification Hubs REST APIs documentation.

Refer: Use REST APIs from a backend