Xamarin Push Notifications Using Azure Notifications Hub

One of the biggest problems when working with multiple mobile platforms is push notifications. You need to understand how to interact with every one of the backends from Apple, Google and Microsoft to be able to send the notifications to the devices. Azure has an awesome service called Notifications Hub, part of Service Bus, that help you to simplify this task.

Configuring a new Notifications Hub

To start, you will need to configure a new notification hub for your application. To make this you need to go to the Azure Management Portal and set up a new notifications hub, inside the Service Bus as in the following:

Xamarin Push Notifications Using Azure Notifications Hub

You  will need to indicate a service name, the execution region for the service and a namespace name. After completing all data and tap on the "CREATE A NEW NOTIFICATION HUB" button in the bottom-right corner, you can access it from the Service bus menu in the left side of the screen.

For the first time, you will need to configure each mobile platform store credentials, so your new Notification Hub could communicate with each of them:

  • Apple Store: You need to generate a digital certificate with your Apple developer account.
  • Windows Phone (MPNS): You need to generate a digital certificate.
  • Windows y Windows Phone (WNS): You need to enter the application Live Services SID and secret key.
  • Google Play: You need to get a Google Cloud Messaging (GCM) key. 

There are two possible configurations for Windows Phone. The first one, the Microsoft Push Notifications Service (MPNS) is for applications compiled for Windows Phone 7.X or Windows Phone 8.0. The second, Windows Notifications Service (WNS) was introduced for Windows Store applications and is used also in Windows Phone 8.1 applications.

In this article you can see how to get and configure the Notification Hub with Windows (WNS) and Android (GCM). For the Apple Store, the needs for a pay developer account blocks me at this moment to create the certificate.

WNS

To get the secret key and SID of your application Live Services, you need to first associate your application with the store from Visual Studio and reserve a name for it. This doesn't mean that the app needs to be published. After this, you can go to the Windows Store to reserve an application and open the Services step:

Xamarin Push Notifications Using Azure Notifications Hub


There you will find a link to the Live Services site:

Xamarin Push Notifications Using Azure Notifications Hub


In the live Services site for our application, we need to take note of the package SID and client secret key:

Xamarin Push Notifications Using Azure Notifications Hub


This data is what you need to add to the Notifications Hub WNS configuration section:

Xamarin Push Notifications Using Azure Notifications Hub


And you are ready! Now all you need to do is configure the work with the Windows Notifications Service and send notifications to your Windows Store and Windows Phone app.

Google Play

Google asks us to also make some configuration in the Google developer console. Note here you don't need to have a paid account actually to do that.

The first step is to go to the Google developer console website: http://console.developers.google.com and create a new project for your application. In this sample, I create a new project called NSHubSample and assigned an id nshubsample-xamarin to it.

Inside the new project, you need to go to the APIs section and activate Google Cloud Messaging for the Android API. You can then go to the credentials section and create a new server key (public key) and take note of it:

Xamarin Push Notifications Using Azure Notifications Hub


Now you can return to the Notifications Hub configuration and enter this API KEY in the Google cloud messaging key configuration:

Xamarin Push Notifications Using Azure Notifications Hub


And now you have everything configured to send notifications to Android, Windows and Windows Phone applications. As a last step in Azure Management Portal, you will need the connection strings to communicate between your backend and the Notifications Hub and to register the apps at runtime with the Notifications Hub. You can find the connection strings in the main page of the Notification Hub, in the Dashboard, clicking on "View Connection String" link:

Xamarin Push Notifications Using Azure Notifications Hub


After clicking it, a new popup is shown with the two connection strings:

Xamarin Push Notifications Using Azure Notifications Hub

Take note of both since you will need them in a short time. Now it is time to start working on the client side of the things. With Xamarin, you will create a new Android, Windows and Windows Phone application and configure them to register with their cloud service and send the registration information to the Notifications Hub when needed. Also you will prepare them to receive notifications.

Getting the apps notification channels

With Xamarin you are able to share lots of code, the one needed for registering your device/app with every cloud provider is unique for each platform.

Windows XAML

To start, you will need to install the "Windows Azure Service Bus Managed for Windows Store and Windows Phone" NuGet package in your project. This package will make it easier to send to your Notifications Hub to the app notification channels.

To obtain these channels, you can use the PushNotificationChannelManager class, from the Windows.Networking.PushNotifications namespace. Inside that class, you will find a method called CreatePushNotificationChannelForApplicationAsync. When you get your notification channel, you need to send it to your Notifications Hub, creating a new instance of the NotificationHub class and sending as parameters:

  • The name of your notification hub, in this sample: nhsampledemo.
  • One of the connection strings you get before, the one called: DefaultListenSharedAccessSignature

Finally, you invoke the RegisterNativeAsynd method, passing the URI obtained as your notification channel:

private async void InitNotificationsAsync()  
{  
    var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();  
  
    var hub = new NotificationHub("nhsampledemo", "<Endpoint listen>");  
    var result = await hub.RegisterNativeAsync(channel.Uri);  
}

And that's all you need in Windows XAML to obtain and configure the notifications. 

Android 

With Android, you will need to first install a new component in your Xamarin project, called "Google Cloud Messaging Client". To make this, right-click the Components folder in your Xamarin Android project and select the "Get more components" option. A new window popup lists the available components. Search for "Google Cloud Messaging Client" and click over it, in the details page, click on the "Add to app" Green button:

Xamarin Push Notifications Using Azure Notifications Hub


To register your application, you will also need the Google developer console project ID you created before. You can find it in the main page of the project:

Xamarin Push Notifications Using Azure Notifications Hub


Now you can start writing the code needed to register your app with Google Cloud Messaging. For example, in the OnCreate method of your first view:

private void RegisterWithGCM()  
{  
    GcmClient.CheckDevice(this);  
    GcmClient.CheckManifest(this);  
    GcmClient.Register(this, "<PROJECT ID>");  
}

The CheckDevice method ensures the device the app is running on support Google Play services, needed for notifications. CheckManifest method ensures your manifest is correctly configured, if needed and the Register method registers this application with the Project ID you get from Google developer console previously.

Now you have your Android app registered, but for being able to receive notifications you need to create a background service able to process them when they arrive to the device. To make this you will need the following two classes:

  • MyBroadcastReceiver, registered as the receiver of Google Cloud Messaging notifications.
  • GcmService, the actual implementation of the code needed to receive and process notifications.

First, you need to add some permissions attributes to the namespace of the classes:

[assembly: Permission(Name = "notificationsHub.Android.permission.C2D_MESSAGE")]  
[assembly: UsesPermission(Name = "notificationsHub.Android.permission.C2D_MESSAGE")]  
[assembly: UsesPermission(Name = "com.google.android.c2dm.permission.RECEIVE")]  
  
[assembly: UsesPermission(Name = "android.permission.GET_ACCOUNTS")]  
[assembly: UsesPermission(Name = "android.permission.INTERNET")]  
[assembly: UsesPermission(Name = "android.permission.WAKE_LOCK")]  
  
namespace NotificationsHub.Android  

The class MyBroadcastReceiver also needs some more attributes to be properly identified. MyBroadcastReceiver will inherit from GcmBroadcastReceiverBase<T> being T = your own GcmService class:

[BroadcastReceiver(Permission = Gcm.Client.Constants.PERMISSION_GCM_INTENTS)]  
[IntentFilter(new string[] { Gcm.Client.Constants.INTENT_FROM_GCM_MESSAGE }, Categories = new string[] { "@PACKAGE_NAME@" })]  
[IntentFilter(new string[] { Gcm.Client.Constants.INTENT_FROM_GCM_REGISTRATION_CALLBACK }, Categories = new string[] { "@PACKAGE_NAME@" })]  
[IntentFilter(new string[] { Gcm.Client.Constants.INTENT_FROM_GCM_LIBRARY_RETRY }, Categories = new string[] { "@PACKAGE_NAME@" })]  
public class MyBroadcastReceiver : GcmBroadcastReceiverBase<GcmService>  
{  
    public static string[] SENDER_IDS = new string[] { "<PROJECT ID>" };  
  
    public const string TAG = "MyBroadcastReceiver-GCM";  
}

The GcmService class needs to have the service attribute and inherit from the GcmServiceBase class. Before beginning to type in the code, you will need to add a new NuGet package to your project, called "Xamarin.NotificationHub":  

[Service] //Must use the service tag  
public class GcmService : GcmServiceBase  
{  
    public static string RegistrationID { get; private set; }  
    private NotificationHub Hub { get; set; }  
  
    public GcmService() : base("<PROJECT ID>")  
    {  
        Log.Info(MyBroadcastReceiver.TAG, "GcmService() constructor");  
    }  
  
    protected override async void OnRegistered(Context context, string registrationId)  
    {  
        Log.Verbose(MyBroadcastReceiver.TAG, "GCM Registered: " + registrationId);  
        RegistrationID = registrationId;  
  
        createNotification("GcmService-GCM Registered...", "The device has been Registered, Tap to View!");  
  
        Hub = new NotificationHub("nhsampledemo", "<listen endpoint>");  
        try  
        {  
            await Hub.UnregisterAllAsync(registrationId);  
        }  
        catch (Exception ex)  
        {  
            Debug.WriteLine(ex.Message);  
            Debugger.Break();  
        }  
  
        try  
        {  
            List<string> TAGS = new List<string>()  
            {  
            };  
            var hubRegistration = await Hub.RegisterNativeAsync(registrationId, TAGS);  
        }  
        catch (Exception ex)  
        {  
            Debug.WriteLine(ex.Message);  
            Debugger.Break();  
        }  
    }  
  
    protected override void OnMessage(Context context, Intent intent)  
    {  
        Log.Info(MyBroadcastReceiver.TAG, "GCM Message Received!");  
  
        var msg = new StringBuilder();  
  
        if (intent != null && intent.Extras != null)  
        {  
            foreach (var key in intent.Extras.KeySet())  
                msg.AppendLine(key + "=" + intent.Extras.Get(key).ToString());  
        }  
  
        string messageText = intent.Extras.GetString("msg");  
        if (!string.IsNullOrEmpty(messageText))  
        {  
            createNotification("New hub message!", messageText);  
            return;  
        }  
  
        createNotification("Unknown message details", msg.ToString());  
    }  
  
    void createNotification(string title, string desc)  
    {  
        var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;  
        var uiIntent = new Intent(this, typeof(FirstView));  
        var notification = new Notification(Android.Resource.Drawable.Icon, title);  
        notification.Flags = NotificationFlags.AutoCancel;  
        notification.SetLatestEventInfo(this, title, desc, PendingIntent.GetActivity(this, 0, uiIntent, 0));  
  
        notificationManager.Notify(1, notification);  
    }  
  
    protected override void OnError(Context context, string errorId)  
    {  
        //Manage errors  
    }  
  
    protected override void OnUnRegistered(Context context, string registrationId)  
    {  
        //Manage unregistering services.  
    }  
}

You can see four important methods in this class: OnRegistered, OnMessage, OnError and OnUnRegistered:

  • OnRegistered is executed when you register the application with the GcmClient class Register method.
  • OnMessage is executed when a new notification is received. Here the code processes the received message and shows it as a toast notification.
  • OnError, launches if an error is encountered.
  • OnUnRegistered, executes when this instance is unregistered.

iOS

In iOS you will work in the AppDelegate class. In the first place, you need to add a new component to your iOS project, in the same way you did before with Google Cloud Messaging Client for Android. This time you also need to find the Azure Mobile Services component. 

Also, we need to add a reference to the WindowsAzure.Messaging library for iOS. At this time there isn't a NuGet package available for this, but the  Android NuGet Package of Xamarin.NotificationHub is included in the monotouch folder, so you can pick it right from there. The complete name of the library is ByteSmith.WindowsAzure.Messaging.iOS.dll.

Now you can start adding code to the AppDelegate class in your iOS project. First, add a call to RegisterForRemoteNotificationTypes to the FinishedLaunching method:

UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes); 

Then call the method RegisteredForRemoteNotifications is called, so you need to override it:

public override async void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)  
{  
    Hub = new NotificationHub(ConnectionString, NotificationHubPath);  
    await Hub.UnregisterAllAsync(deviceToken.ToString());  
    await Hub.RegisterNativeAsync(deviceToken.ToString());  
}

When a new notification is received, the method ReceivedRemoteNotification is called with the needed info so you can process the notification:  

public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)  
{  
    ProcessNotification(userInfo, false);  
}  
  
void ProcessNotification(NSDictionary options, bool fromFinishedLaunching)  
{  
    // Check to see if the dictionary has the aps key.  This is the notification payload you would have sent  
    if (options != null && options.ContainsKey(new NSString("aps")))  
    {  
        //Get the aps dictionary  
        NSDictionary aps = options.ObjectForKey(new NSString("aps")) as NSDictionary;  
  
        string alert = string.Empty;  
  
        if (aps.ContainsKey(new NSString("alert")))  
            alert = (aps[new NSString("alert")] as NSString).ToString();  
  
        //If this came from the ReceivedRemoteNotification while the app was running,  
        // we of course need to manually process things like the sound, badge and alert.  
        if (!fromFinishedLaunching)  
        {  
            //Manually show an alert  
            if (!string.IsNullOrEmpty(alert))  
            {  
                UIAlertView avAlert = new UIAlertView("Notification", alert, null, "OK", null);  
                avAlert.Show();  
            }  
        }  
    }  
}

And that is all you need to do with the client apps. Now let's move to the server side.

Server

In this sample source code, you will find a little WPF application acting as the server. This WPF app only has three buttons, to launch a notification to every platform registered. To make this, you need to add two NuGet packages to the project:

  • Microsoft.WindowsAzure.ConfigurationManager
  • WindowsAzure.ServiceBus 

Installing WindowsAzure.ServiceBus, installs automatically the other one as part of the dependencies. After the installation, you can start writing the "backend" code. For example, this code launches a notification for WNS:

private async void Send_Windows(object sender, RoutedEventArgs e)  
{  
    NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString("<FULLSHAREDACCESS ENDPOINT>", "nhsampledemo");  
    var toast = @"<toast><visual><binding template=""ToastText01""><text id=""1"">Hello from a .NET App!</text></binding></visual></toast>";  
    await hub.SendWindowsNativeNotificationAsync(toast);  
}

This one launches a notification for Android using Google Cloud Messaging:

private async void Send_Android(object sender, RoutedEventArgs e)  
{  
    NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString("<FULLSHAREDACCESS ENDPOINT>", "nhsampledemo");  
    var message = "{ \"data\" : {\"msg\":\"Hello from Azure!\"}}";  
    var result = await hub.SendGcmNativeNotificationAsync(message);  
}

And finally this is the iOS one: 

private async void Send_iOS(object sender, RoutedEventArgs e)  
{  
    NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString("<FULL ENDPOINT>", "nhsampledemo");  
    var alert = "{\"aps\":{\"alert\":\"Hello from .NET!\"}}";  
    await hub.SendAppleNativeNotificationAsync(alert);  
} 

In all three cases, you need to create a new NotificationHubClient, using the name of the notification hub (nhsampledemo) and the Full access connection string you get from the Notification Hub dashboard before. Then you create the notification payload and use a method specific for each platform to send the notification to the Notifications Hub, who sends it to the needed platform.

To learn more details about how to write various notifications for each platform, you can have a look here:

And that's all! Now you have a Xamarin solution with notifications and understand how to create and configure an Azure Notifications Hub to use it with your cross platform solutions.

All the code for this example is available publicly in GitHub, feel free to use it any way you need. 


Similar Articles
DevsDNA
Development, training and mentoring company focused on Mobile and Augmented reality