Get started with Notification Hubs for Xamarin.Android apps


This tutorial shows you how to use Azure Notification Hubs to send push notifications to a Xamarin.Android application. You'll create a blank Xamarin.Android app that receives push notifications by using Firebase Cloud Messaging (FCM). When you're finished, you'll be able to use your notification hub to broadcast push notifications to all the devices running your app. The finished code is available in the NotificationHubs app sample.

This tutorial demonstrates the simple broadcast scenario in using Notification Hubs.

Before you begin

The goal of this article is to help you get started using Azure Notification Hubs as quickly as possible. The article presents a very simple broadcast scenario that focuses on the basic concepts of Notification Hubs.

If you are already familiar with Notification Hubs, you might want to select another topic from the left navigation or go to the relevant articles in the "Next steps" section.

We take your feedback seriously. If you have any difficulty completing this topic, or if you have recommendations for improving this content, we invite you to provide feedback at the end of the article.

The completed code for this tutorial can be found on GitHub here.


This tutorial requires the following:

Completing this tutorial is a prerequisite for all other Notification Hubs tutorials for Xamarin.Android apps.


To complete this tutorial, you must have an active Azure account. If you don't have an account, you can create a free trial account in just a couple of minutes. For details, see Azure Free Trial.

Enable Firebase Cloud Messaging

  1. Sign in to the Firebase console. Create a new Firebase project if you don't already have one.
  2. After you create your project, select Add Firebase to your Android app. Then follow the instructions that are provided.

    Add Firebase to your Android app

  3. In the Firebase console, select the cog for your project. Then select Project Settings.

    Select Project Settings

  4. Select the General tab in your project settings. Then download the google-services.json file that contains the Server API key and Client ID.
  5. Select the Cloud Messaging tab in your project settings, and then copy the value of the Legacy server key. You use this value to configure the notification hub access policy.

Configure your notification hub

  1. Sign in to the Azure portal.

  2. Select Create a resource > Web + Mobile > Notification Hub.

    Azure portal - create a notification hub

  3. In the Notification Hub box, type a unique name. Select your Region, Subscription, and Resource Group (if you have one already).

    If you don't already have a service bus namespace, you can use the default name, which is created based on the hub name (if the namespace name is available).

    If you already have a service bus namespace that you want to create the hub in, follow these steps

    a. In the Namespace area, select the Select Existing link.

    b. Select Create.

    Azure portal - set notification hub properties

  4. After you've created the namespace and notification hub, open it by selecting All resources and then select the created notification hub from the list.

    Azure portal - notification hub portal page

  5. Select Access Policies from the list. Note the two connection strings that are available to you. You need them to handle push notifications later.


    Do NOT use the DefaultFullSharedAccessSignature in your application. This is meant to be used in your back-end only.

    Azure portal - notification hub connection strings

  1. Choose the Configure tab at the top, enter the API Key value you obtained in the previous section, and then select Save.


Your notification hub is configured to work with FCM, and you have the connection strings to both register your app to receive notifications and to send push notifications.

Connect your app to the notification hub

First, you create a new project.

  1. In Visual Studio, choose New Solution > Android App and then select Next.

    Visual Studio- Create new Android project

  2. Enter your App Name and Identifier. Choose the Target Platforms you want to support and then choose Next and Create.

    Visual Studio- Android app configuration

    This creates a new Android project.

  3. Open the project properties by right-clicking your new project in the Solution view and choosing Options. Select the Android Application item in the Build section.

    Ensure that the first letter of your Package name is lowercase.


    The first letter of the package name must be lowercase. Otherwise, you will receive application manifest errors when you register your app for push notifications below.

    Visual Studio- Android project options

  4. Optionally, set the Minimum Android version to another API Level.
  5. Optionally, set the Target Android version to another API version that you want to target (must be API level 8 or higher).
  6. Choose OK and close the Project Options dialog.

Add the required packages to your project

  1. Right-click your project and select Add > Add NuGet Packages
  2. Search for Xamarin.Azure.NotificationHubs.Android and add it to the project.
  3. Search for Xamarin.Firebase.Messaging and add it to the project.

Set up notification hubs in your project

  1. Gather the following information for your Android app and notification hub:

    • Listen connection string: On the dashboard in the Azure portal, choose View connection strings. Copy the DefaultListenSharedAccessSignature connection string for this value.
    • Hub name: This is the name of your hub from the Azure portal. For example, mynotificationhub2.
  2. Create a Constants.cs class for your Xamarin project and define the following constant values in the class. Replace the placeholders with your values.

        public static class Constants
           public const string ListenConnectionString = "<Listen connection string>";
           public const string NotificationHubName = "<hub name>";
  3. Add the following using statements to MainActivity.cs:

        using Android.Util;
  4. Add an instance variable to MainActivity.cs that will be used to show an alert dialog when the app is running:

        public const string TAG = "MainActivity";
  5. Add the following code to OnCreate in MainActivity.cs after base.OnCreate(savedInstanceState):

        if (Intent.Extras != null)
            foreach (var key in Intent.Extras.KeySet())
                    var value = Intent.Extras.GetString(key);
                    Log.Debug(TAG, "Key: {0} Value: {1}", key, value);
  6. Right-click your project and add the google-services.json that you downloaded from your Firebase project earlier. Right-click the added file and set the build action to GoogleServicesJson

    Visual Studio- Configure google-services.json

  7. Create a new class, MyFirebaseIIDService.

  8. Add the following using statements to MyFirebaseIIDService.cs:

        using System;
        using Android.App;
        using Firebase.Iid;
        using Android.Util;
        using WindowsAzure.Messaging;
        using System.Collections.Generic;
  9. In MyFirebaseIIDService.cs, add the following above the class declaration, and have your class inherit from FirebaseInstanceIdService:

        [IntentFilter(new[] { "" })]
        public class MyFirebaseIIDService : FirebaseInstanceIdService
  10. In MyFirebaseIIDService.cs, add the following code:

        const string TAG = "MyFirebaseIIDService";
        NotificationHub hub;
        public override void OnTokenRefresh()
            var refreshedToken = FirebaseInstanceId.Instance.Token;
            Log.Debug(TAG, "FCM token: " + refreshedToken);
        void SendRegistrationToServer(string token)
            // Register with Notification Hubs
            hub = new NotificationHub(Constants.NotificationHubName,
                                      Constants.ListenConnectionString, this);
            var tags = new List<string>() { };
            var regID = hub.Register(token, tags.ToArray()).RegistrationId;
            Log.Debug(TAG, $"Successful registration of ID {regID}");
  11. Create another new class for your project, name it MyFirebaseMessagingService.

  12. Add the following using statements to MyFirebaseMessagingService.cs.

        using System;
        using System.Linq;
        using Android;
        using Android.App;
        using Android.Content;
        using Android.Util;
        using Firebase.Messaging;
  13. Add the following above your class declaration, and have your class inherit from FirebaseMessagingService:

        [IntentFilter(new[] { "" })]
        public class MyFirebaseMessagingService : FirebaseMessagingService
  14. Add the following code to MyFirebaseMessagingService.cs:

        const string TAG = "MyFirebaseMsgService";
        public override void OnMessageReceived(RemoteMessage message)
            Log.Debug(TAG, "From: " + message.From);
            if(message.GetNotification()!= null)
                //These is how most messages will be received
                Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);
                //Only used for debugging payloads sent from the Azure portal
        void SendNotification(string messageBody)
            var intent = new Intent(this, typeof(MainActivity));
            var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
            var notificationBuilder = new Notification.Builder(this)
                        .SetContentTitle("FCM Message")
            var notificationManager = NotificationManager.FromContext(this);
            notificationManager.Notify(0, notificationBuilder.Build());
  15. Run your app on your device or loaded emulator

Send notifications from the portal

You can test receiving notifications in your app with the Test Send option in the Azure portal. This sends a test push notification to your device.

Azure portal - Test Send

Push notifications are normally sent in a back-end service like Mobile Services or ASP.NET through a compatible library. If a library is not available for your back-end you can also use the REST API directly to send notification messages.

Here is a list of some other tutorials that you may want to review for sending notifications:

Next steps

In this simple example, you broadcasted notifications to all your Android devices. In order to target specific users, refer to the tutorial Use Notification Hubs to push notifications to users. If you want to segment your users by interest groups, you can read Use Notification Hubs to send breaking news. Learn more about how to use Notification Hubs in Notification Hubs Guidance and in the Notification Hubs How-To for Android.