loading...

Send Push Notification from a webhook endpoint using Azure notification hub

mcc_ahmed profile image Ahmed Fouad Originally published at Medium on ・3 min read

Webhooks are user-defined HTTP callbacks where the provider calls the client's web service to notify him when a new event occurred by contrast to the API where the client calls the provider to GET some information or POST new data.

For example if you are building a continuous integration system and you want to build and create an app package every time a commit is done to the Master branch.

So your build tool must get notified when a new commit is made and this can only happen using webhooks, your build server will register his endpoint in the git server and the git server will call your build server registered endpoint and therefore your build server will pull the new code and create a build.

Another example and it is the one that we will do together in this article, you know that I use ko-fi.com to gather donations to keep this blog running and buy tools to enhance the article quality, now I wanna receive a push notification on my mobile when a new donation is made so how I will do this.

Step1: Create an Azure Lambda Function to be used as a webhook endpoint.

This is a very straight forward open visual studio and create new Azure Lambda Function project.

it is very straight forward I just receive the post request body, decode it and parse it to get the payload according to ko-fi.com documentation.

Next, I use the azure notification hub to send an FCM notification with the message content.

please remember to install the azure notification hub nugget package

Microsoft.Azure.NotificationHubs 3.3.0

var notificationHubClient = new NotificationHubClient(connectionString, hubName);

the notification hub constructor takes 2 parameters connection string and hub name, we will see how we get them in step 2.

var notificationResult = await notificationHubClient.SendNotificationAsync(

new FcmNotification("{\"data\":{\"message\":\"" + payload.Message + "\"}}"));

we create and send the FcmNotification with a JSON payload, please check the FCM documentation for more.

Step2: Create Azure Notification Hub

using Azure Portal, create a new Notification Hub resource, then go to the resource page and navigate to Acess Policies to get the connection string.

The DefaultFullSharedAccessSignature is the first parameter in the NotificationHubClient constructor and the second parameter is just the resource name which is the hub name.

var notificationHubClient = new NotificationHubClient("Endpoint=sb://xxx.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=53Mxxxx=", "kofihub");

var notificationResult =

await notificationHubClient.SendNotificationAsync(

new FcmNotification("{\"data\":{\"message\":\"" + payload.Message + "\"}}"));

Now you can publish your azure lambda function from the visual studio by right-clicking on it then click publish.

Step 3 Create a Firebase project

I think to create a new project on firebase.google.com is very straightforward and no need to put screenshots for it.

just remember after the project is created go to the settings page and trap the web API key and put it in the azure notification hub fcm settings

Step 4 Add fcm push notification support to your xamarin app

you can follow Microsoft excellent documentation to do this

Add push notifications to your Xamarin.Forms app - Azure Mobile Apps

Step 5 Register your azure function endpoint as a webhook callback

In Azure Portal Go to your FunctionApp resource and navigate your function then click “Get Function URL “

now provide the URL to your 3rd party provider, in my case it is ko-fi.com

and now we are done.

I would like to recommend you the Practical Azure Functions: A Guide to Web, Mobile, and IoT Applications book, it is really one of the best books that will show you the power of lambda functions.

And please if you like this article, you can support me on ko-fi.com it will only cost you 3 USD but it will help me publishing new content and using more professional tools to create better quality articles, I am sure that 3 USD is not a lot.

Buy AhmedFouad a Coffee. ko-fi.com/ahmedfouad

Posted on by:

mcc_ahmed profile

Ahmed Fouad

@mcc_ahmed

I am a full stack .net dev ,FP antisuaste and currently i am writting some articles about fp using .net framework

Discussion

markdown guide