Scenario
- In Business central we have a “Messages” table where there are messages to be dispatched to mobile devices;
- In the Business Central we have a page where the user sends a push notification to the sales agents;
- The sales agents receive the Push Notification containing the message in the Android App;
THE FLOW

RECIPE
- A simple Xamarin Forms app;
- An Azure Function;
- An Azure Notification Hub account;
- A Firebase Google Cloud Messaging or a Google Cloud Messaging account (*);
- A Business Central Extension;
- In this sample I am not using Azure Vault to store passwords and I just use settings.json, you should when in production environment 🙂
* Pay attention, GCM Google Cloud Messaging is not FCM Firebase Cloud Messaging. FCM and GCM are two different services to send push notifications available on the Google Messaging platform. The GCM is deprecated and the FCM is the new one. In this tutorial I cover both services since unfortunately you might have, as me, some apps already configured with GCM that cannot be migrated to FCM.
There’re two kind of notifications in FCM: Data messages and Notification messages.
If you are using the Data messages you will be in charge of handling the received message and present a notification to the user (if needed of course). But in this case you might miss notifications when your app is closed.
If you are using Notification Messages, FCM is handling the message for you and directly displays a notification if the app is in background/closed.
Check here for differences: https://developers.google.com/cloud-messaging/faq
XAMARIN FORMS APP
The Xamarin Forms sources are available from GitHub.
https://github.com/avalonit/XamBcPushNotification
The solution I made for you in GitHub is already configured. If you start with your own project you have to do a lot of things like: add Xamarin.GooglePlayServices.Base, Xamarin.Firebase.Messaging Xamarin.Azure.NotificationHubs.Android nuget packages, add google-services.json to your project and set it as Build Action = “GoogleServiceJson”, change AndroidManifest.xml and properly add <uses-permissions> and <receiver> tags.
You have the full procedure available here: https://docs.microsoft.com/it-it/azure/notification-hubs/xamarin-notification-hubs-push-notifications-android-gcm.
As in my solution, the packages list should appears as in picture, the google-service.json must be properly added and Android.Manifest.xml modified as shown below.

Te Xamarin Forms project is a simple single page app that just register and waits for a push notification.
The Xamarin Forms basically consists in three functions and all of then are in the AppDelegate.cs file.
Firstly the app checks the presence on device of Google Services and registers itself to the Firebase FCM Notification Service..

MyFirebaseMessagingService is the function that manage the push notification behaviour.

The Azure Hub Notification endpoints have to be configured in AzureNotificationConstants.cs
Documentation about different ways of implementing Firebase Notification in Xamarin Android app is available here: https://docs.microsoft.com/it-it/xamarin/android/data-cloud/google-messaging/remote-notifications-with-fcm?tabs=macos
AZURE FUNCTION
The Xamarin Forms sources are available from GitHub:
https://github.com/avalonit/AzBcPushNotification-
The Azure Function is pretty simple too. It wait for a GET request with the message as a query parameter. When it is called it dispatch the message by using NotificationHubClient.

BUSINESS CENTRAL EXTENSION
The Business Central extension sources are available from GitHub:
https://github.com/avalonit/ExBcNotificationHub
The extension consists in very simple table with a page card and a page list that allow CRUD for records.
The codeunit “Cod70359948.ALV Send Push Notification.al” contains the SendPush function that calls the Azure Function to send the messages.

CONFIGURE GOOGLE GCM (***DEPRECATED*** SEE GOOGLE FCM SECTION FOR NEW APPS)
Take a seat, be patient, drink a calming herbal tea because this chapter is quiet long 🙂
Please notice that the procedure of google GCM often changes so, at the time you are reading, this section might already be out of date. In case check this link https://docs.microsoft.com/it-it/azure/notification-hubs/notification-hubs-android-push-notification-google-gcm-get-started#creating-a-project-that-supports-google-cloud-messaging
Firstly, open Google Developer Console (https://console.developers.google.com/cloud-resource-manager) and access with your credentials.

Insert your project Name and click “Create”. Click on alert button on top bar and select the project from the list. You are redirected in the project dashboard page.
In the project dashboard there is the “Project Number”. Note this number because you will need in next steps.

Find the API section inside dashboard page and click on “Go to APIs overview”.

You are now in API page, here click on “Enable APIs and Services”.

Search, find and select “Google Cloud Messaging” or “Cloud Messaging”.

Click “Enable”

Select “Create credentials” on the tool bar.

You are redirected in “Add credential to your project” page, here you click “API key” shortcut.

You are redirected in “API Key” page. Here select Create/Save. Here configure as in picture, by selecting “IP addresses” option with 0.0.0.0/0 address. For your security here you should insert the IP address your azure function is assigned to when created in Azure Portal.

A dialog appears with your own API Key. Save it somewhere because we will use it in Azure. This KEY is used on Azure Portal to authenticate on GCM services to send push notifications.

CONFIGURE GOOGLE FCM (THE NEW FIREBASE CLOUD MESSAGING)
Go to FCM Firebase Cloud Messaging Console: https://firebase.google.com/console/
Select previous created project and select “Add Firebase to your Android App”.

Digit your android package name.

Click on “Register app” and download your “google-services.json”.

Skip the test by ignoring app verification.

Go to “Project Overview” and select “Project settings”.

Write down the Web API Key somewhere, you will need it when configuring your Xamarin Forms app.

Go to Cloud Messaging tab, there write down the “Server key”, you will need it when configuring in Azure Portal.

CONFIGURE MICROSOFT AZURE HUB NOTIFICATION
In Azure Portal https://portal.azure.com/, you create are resource “Notification Hub”.

Create the resource group, insert your namespace and hub name.

Wait Azure completes the creation of the resource, then edit the notification hub just created and go to “Settings” -> “Google (GCM/FCM)” section.
If you are using GCM paste here your GCM “Api key” as saved in previous section (GCM).
If you are using FCM paste here FCM “Server Key” as saved in previous section (FCM).

CONFIGURE XAMARIN FORM AND AZURE FUNCTION
In your Xamarin Forms solution, open AzureNotificationConstants.cs and copy the ListenConnectionString and the hub name.
Open Azure Function in Visual Studio Code, edit the “local.settings.json” file and copy the connection string and HubName here too.

AND WHAT’S NEXT?
This solution send broadcast messages, I will publish soon a complete solution where it is possibile to send a message to a specific user.
LAST UPDATE
18th April 2020