Send Push Notification from a webhook endpoint using Azure notification hub
Tags: azure, dotnet, csharp, xamarin
Published: 2020-04-05 12:28:37

---
title: Send Push Notification from a webhook endpoint using Azure notification hub
published: true
date: 2020-04-04 21:43:55 UTC
tags: azure,dotnet,csharp,xamarin
canonical_url: 
---

![](https://cdn-images-1.medium.com/max/1024/1*wAV_oAkr-GFqE9EwLiYebQ.png)

> **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](https://ko-fi.com/ahmedfouad) 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.

{% gist https://gist.github.com/TheFo2sh/21705b410e07b121d72c89df6e28bfdf %}

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.

![](https://cdn-images-1.medium.com/max/1024/1*GwKSMQldq5zzIuIAoRhG7g.png)

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](https://www.nuget.org/packages/Microsoft.Azure.NotificationHubs)

```
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.

![](https://cdn-images-1.medium.com/max/1024/1*YbGRl772z68ZIlwhPitrBg.png)

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

![](https://cdn-images-1.medium.com/max/1024/1*vlxQN5X5S1l8jZOsKsfAjQ.png)

![](https://cdn-images-1.medium.com/max/1024/1*1RnZIl67EM-JfYdq_EhbCw.png)

### 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](https://docs.microsoft.com/en-us/azure/app-service-mobile/app-service-mobile-xamarin-forms-get-started-push)

### 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 “

![](https://cdn-images-1.medium.com/max/1024/1*CbQrDa4hXWpqguQspxfvmg.png)

now provide the URL to your 3rd party provider, in my case it is [ko-fi.com](https://ko-fi.com/ahmedfouad)

![](https://cdn-images-1.medium.com/max/1024/1*f_Zlqbcv29k3SyMV28P_Sg.png)

and now we are done.

I would like to recommend you the [Practical Azure Functions: A Guide to Web, Mobile, and IoT Applications](https://amzn.to/2R8JZZT) 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](https://ko-fi.com/ahmedfouad) 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](https://ko-fi.com/ahmedfouad)