Professional Documents
Culture Documents
Apps
Overview
A push notification is used to deliver information, such as a message, from a backend system to an
application on a mobile device in order to increase application engagement and usage. The notification can
be sent at anytime, even when the user is not actively using the targeted application.
Backend systems send push notifications to mobile devices through Platform Notification Systems (PNS),
as shown in the following diagram:
To send a push notification, the backend system contacts the platform-specific PNS to send a notification
to a client application instance. This significantly increases the complexity of the backend when cross-
platform push notifications are required, because the backend must use each platform-specific PNS API
and protocol.
Azure Notification Hubs eliminate this complexity by abstracting the details of the different platform
notification systems, allowing a cross-platform notification to be sent with a single API call, as shown in the
following diagram:
To send a push notification, the backend system only contacts the Azure Notification Hub, which in turn
communicates with the different platform notification systems, therefore decreasing the complexity of the
backend code that sends push notifications.
Azure Mobile Apps have built-in support for push notifications using notification hubs. The process for
sending a push notification from an Azure Mobile Apps instance to a Xamarin.Forms application is as
follows:
1. The Xamarin.Forms application registers with the PNS, which returns a handle.
2. The Azure Mobile Apps instance sends a notification to its Azure Notification Hub, specifying the
handle of the device to be targeted.
3. The Azure Notification Hub sends the notification to the appropriate PNS for the device.
4. The PNS sends the notification to the specified device.
5. The Xamarin.Forms application processes the notification and displays it.
The sample application demonstrates a todo list application whose data is stored in an Azure Mobile Apps
instance. Every time a new item is added to the Azure Mobile Apps instance, a push notification is sent to
the Xamarin.Forms application. The following screenshots show each platform displaying the received
push notification:
For more information about Azure Notification Hubs, see Azure Notification Hubs and Add push
notifications to your Xamarin.Forms app on the Azure Documentation Center.
1. Create an Azure Mobile Apps instance. For more information, see Consuming an Azure Mobile App.
2. Create a notification hub. For more information, see Create a Notification Hub on the Azure
Documentation Center.
3. Update the Azure Mobile Apps instance to send push notifications. For more information, see Update
the server project to send push notifications on the Azure Documentation Center.
4. Register with each PNS.
5. Configure the notification hub to communicate with each PNS.
The following sections provide additional setup instructions for each platform.
iOS
The following additional steps must be carried out to use Apple Push Notification Service (APNS) from an
Azure Notification Hub:
1. Generate a certificate signing request for the push certificate with the Keychain Access tool. For
more information, see Generate the Certificate Signing Request file for the push certificate on the
Azure Documentation Center.
2. Register the Xamarin.Forms application for push notification support on the Apple Developer Center.
For more information, see Register your app for push notifications on the Azure Documentation
Center.
3. Create a push notifications enabled provisioning profile for the Xamarin.Forms application on the
Apple Developer Center. For more information, see Create a provisioning profile for the app on the
Azure Documentation Center.
4. Configure the notification hub to communicate with APNS. For more information, see Configure the
notification hub for APNS.
5. Configure the Xamarin.Forms application to use the new App ID and provisioning profile. For more
information, see Configuring the iOS project in Xamarin Studio or Configuring the iOS project in
Visual Studio on the Azure Documentation Center.
Android
The following additional steps must be carried out to use Firebase Cloud Messaging (FCM) from an Azure
Notification Hub:
1. Register for FCM. For more information, see Enable Google Cloud Messaging on the Azure
Documentation Center.
2. Configure the notification hub to communicate with FCM. For more information, see Configure the
Mobile App backend to send push requests using GCM on the Azure Documentation Center.
The following additional steps must be carried out to use the Windows Notification Service (WNS) from an
Azure Notification Hub:
1. Register for the Windows Notification Service (WNS). For more information, see Register your
Windows app for push notifications with WNS on the Azure Documentation Center.
2. Configure the notification hub to communicate with WNS. For more information, see Configure the
notification hub for WNS on the Azure Documentation Center.
The process for implementing push notification support in an iOS application is as follows:
Before an iOS application can receive push notifications, it must register with the Apple Push Notification
Service (APNS), which will generate a unique device token and return it to the application. Registration is
invoked in the FinishedLaunching override in the AppDelegate class:
UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
UIApplication.SharedApplication.RegisterForRemoteNotifications();
...
}
When an iOS application registers with APNS it must specify the types of push notifications it would like to
receive. The RegisterUserNotificationSettings method registers the types of notifications the
application can receive, with the RegisterForRemoteNotifications method registering to receive
push notifications from APNS.
Failing to call the RegisterUserNotificationSettings method will result in push notifications being
silently received by the application.
This method creates a simple notification message template as JSON, and registers the device to receive
template notifications from the notification hub.
// Show alert
if (!string.IsNullOrEmpty(alert))
{
UIAlertView avAlert = new UIAlertView("Notification", alert, null,
"OK", null);
avAlert.Show();
}
}
The userInfo dictionary contains the aps key, whose value is the alert dictionary with the remaining
notification data. This dictionary is retrieved, with the string notification message being displayed in a
dialog box.
If an application isn't running when a push notification arrives, the application will be launched but the
DidReceiveRemoteNotification method won't process the notification. Instead, get the notification
payload and respond appropriately from the WillFinishLaunching or FinishedLaunching overrides.
Android
The process for implementing push notification support in an Android application is as follows:
1. Add the Google Cloud Messaging Client component to the Android project, and import the
Gcm.Client namespace into the MainActivity class.
2. Register with Firebase Cloud Messaging (FCM) in the MainActivity.OnCreate method. For
more information, see Registering with Firebase Cloud Messaging.
3. Register with the Azure Notification Hub in the GcmServiceBase.OnRegistered method. For
more information, see Registering with the Azure Notification Hub.
4. Implement the GcmService.OnMessage method to process incoming push notifications. For more
information, see Displaying the Contents of a Push Notification.
5. Implement the GcmService.OnUnregistered and GcmService.OnError methods to de-
register and process errors from FCM. For more information, see De-registering and Processing
Errors.
Before an Android application can receive push notifications, it must register with FCM, which will generate
a registration token and return it to the application. Registration is invoked in the OnCreate override in the
MainActivity class:
After checking that FCM is supported and that the manifest has the correct information, the application
registers to receive push notifications from FCM. Note that the CurrentActivity property is used to
expose the MainActivity instance, so that the GcmService helper class can perform registration with
the notification hub on the UI thread. This helper class is used to handle interactions with FCM
specifically when registration with FCM succeeds, when the application receives a push notification
message, and when FCM errors occur.
In order to register with FCM, the PushHandlerBroadcastReceiver class defines a public array,
SENDER_IDS, that stores the project number for FCM (obtained from Google Developer Console):
[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 PushHandlerBroadcastReceiver :
GcmBroadcastReceiverBase<GcmService>
{
public static string[] SENDER_IDS = new string[] { "
<INSERT_YOUR_SENDER_ID_HERE>" };
}
The SENDER_IDS array value is used by the GcmService helper class in a call to its base class
constructor:
[Service]
public class GcmService : GcmServiceBase
{
public static string RegistrationToken { get; private set; }
The Google Cloud Messaging Client component then uses the value of the SENDER_IDS property stored in
the base class to register with FCM.
Note that the GcmService class should also contain the following permission requests above the
namespace declaration:
[Service]
public class GcmService : GcmServiceBase
{
...
protected override void OnRegistered(Context context, string
registrationToken)
{
Log.Verbose("PushHandlerBroadcastReceiver", "GCM Registered: " +
registrationToken);
RegistrationToken = registrationToken;
This method stores the returned registration token before invoking the Register method on the UI thread.
The Register method creates a simple notification message template as JSON, and registers the device
to receive template notifications from the notification hub.
The GcmService class implements the GcmServiceBase.OnMessage method, which is invoked when
FCM sends a push notification to the application:
[Service]
public class GcmService : GcmServiceBase
{
...
protected override void OnMessage(Context context, Intent intent)
{
Log.Info("PushHandlerBroadcastReceiver", "GCM Message Received!");
This method retrieves the message from the notification, and sends it to the notification manager for display
by the CreateNotification method.
[Service]
public class GcmService : GcmServiceBase
{
...
protected override void OnUnRegistered(Context context, string
registrationToken)
{
Log.Error("PushHandlerBroadcastReceiver", "Unregistered
RegisterationToken: " + registrationId);
}
protected override void OnError(Context context, string errorId)
{
Log.Error("PushHandlerBroadcastReceiver", "GCM Error: " + errorId);
}
...
}
Before a Universal Windows Platform (UWP) application can receive push notifications it must register with
the Windows Notification Service (WNS), which will return a notification channel. Registration is invoked by
the InitNotificationsAsync method in the App class:
await TodoItemManager.DefaultManager.CurrentClient.GetPush()
.RegisterAsync(channel.Uri, templates);
}
This method gets the push notification channel, creates a notification message template as JSON, and
registers the device to receive template notifications from the notification hub.
The InitNotificationsAsync method is invoked from the OnLaunched override in the App class:
This ensures that the push notification registration is created or refreshed every time the application is
launched, therefore ensuring that the WNS push channel is always active.
When a push notification is received it will automatically be displayed as a toast a modeless window
containing the message.
Summary
This article demonstrated how to use Azure Notification Hubs to send push notifications from an Azure
Mobile Apps instance to a Xamarin.Forms application. Azure Notification Hubs provide a scalable push
infrastructure for sending mobile push notifications from any backend to any mobile platform, while
eliminating the complexity of a backend having to communicate with different platform notification systems.