Implementing the Push Notifications in Xamarin.Forms

Push notifications in Xamarin Forms is an interesting project and that is because each platform implements Push Notifications and the corresponding notifications (toasts) in a different way.

I will show you here using Azure Notification Hubs and Xamarin Forms. You can develop your own code to interact with each notification service, however it is far more work and I strongly recommend Azure Notification Hubs or something similar.

What are Push Notifications?

A small piece of information that is sent out to a single or many users of your app. They can be received when the app is running or in the background. The current size limits are:

  • Apple – 4Kb (256bytes prior to iOS 8)
  • Microsoft – 5Kb
  • Google – 4Kb

Note: Push notifications are delivered on a best effort basis. No confirmation of delivery is ever given. If the phone isn’t on to receive the message it will never arrive, nor will you ever know it didn’t arrive.

How are they sent to devices?

Each company has its own technology to push out notifications. Here is what they each call them

Apple Push Notification Service (APNS)

Google Cloud Messaging (GCM)

Windows Push Notification Services (WNS)

Microsoft Push Notification Service (MPNS) For WinRT and WinSL and now obsolete.

You would send a message to their infrastructure, in a specific format and they send it out to mobile devices.

You can’t send a push notification to their device directly, it will always go through the companies infrastructure. They own the Push Notification network and let you access it as needed. Sending a Push Notification is free, however services like the Azure Notification Hub do have a charge once your volume gets higher. However for low usage it is free.

Push Notifications with Azure Notification Hubs

Push notifications are platform specific, so there is no common code that you can use for all.

The instructions on setting up push notification on Azure for each platform can be quite intensive. You have a lot of work ahead of you. Microsoft have instructions specifically for Xamarin.iOS, Xamarin.Android and UWP. I believe its best you follow these to implement them in your app.

UWP Push Notifications

Xamarin.iOS Push Notifications

Xamarin.Android Push Notifications

That will show you how to send a notification via the Azure Notification Hub and how to receive a push notification on each platform.

Tags

When following the above instructions for each platform you will notice that you can register the device and attach Tags. These tags are used to provide categories in which you want this device to subscribe to. For example you could add in the users email address, to send a Push Notification directly to anyone device registered with that email.

Another example is if the device wants to subscribe to alerts about the sports category in breaking new alerts. You can send a push notification to all devices that registered with the tag sports. You can define or use any tags you want.

Sending Notifications with Custom Information

Normally when you send a notification you get to send a title and/or message and if you do it in the default way, it shows up as expected. However sending more information on a notification is normally required. Hence here is how you add an additional parameter.

UWP

When sending toasts you want to send Adaptive and Interactive Toasts.

var toast = $"<toast launch=\"{args}\"><visual><binding template=\"ToastGeneric\"><text id=\"1\">{title}</text><text id=\"2\">{description}</text></binding></visual></toast>";

This example shows you sending custom arguments in the launch parameter.

iOS

var alert = "{\"aps\":{\"alert\":\"" + description + "\"}, \"args\":{\"launch\":\"" + args + "\"}}";

Android

var notif = "{ \"data\" : {\"message\":\"" + description + "\", \"args\":\"" + args + "\"}}";

 

App Lifecycle When Receiving Push Notifications

 

When an app is in the foreground, some platforms won’t show the notification to the user and expect your app to handle it completely. You may need to show the notification yourself, in which case I recommend you use Toasts Plugin for Xamarin Forms. If your app is in the background, the platform will handle showing the notification, however now you have to handle your app being opened via the user clicking the notification in the notification center.

For more information please read about Push Notifications Lifecycle.

Receiving Custom Arguments

This will show you how to handle incoming notification and also get the incoming custom parameter with it.

UWP

In your App.xaml.cs in your UWP project.

protected override void OnActivated(IActivatedEventArgs args)
{
    switch (args.Kind)
    {
        case ActivationKind.ToastNotification: 
            var toastArgs = args as ToastNotificationActivatedEventArgs;
 
            var launch = toastArgs.Argument);

iOS

In your AppDelegate.cs

public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
{
    NSDictionary args = userInfo.ObjectForKey(new NSString("args")) as NSDictionary;
    if (args.ContainsKey(new NSString("launch")))
    {
        var launch = args[new NSString("launch")] as NSString;

You will want to use ReceivedLocalNotification if you pushed your own local notification while the app was in the foreground, while the Push Notification was received.

Android

In MainActivity.cs, I put LaunchMode = LaunchMode.SingleTop in the Activity parameter list as I don’t want a new instance of MainActivity created each time I launch the app. In doing so we can now receive the notification information through the below code.

protected override void OnNewIntent(Intent intent)
{
    if (intent != null)
    {
        var args = intent.GetStringExtra("args");

Troubleshooting

Why am I not receiving Push Notifications?

Push notifications will come in over your Mobile Phone data connection or WiFi. If you are connected to WiFi which most people are when developing, your WiFi router or ISP may block port certain ports. Make sure this is unblocked otherwise you will not receive push notifications.

  • Apple (APNS) – 2195
  • Google (GCM) – 5228-5230
  • Microsoft (MPNS) – 80/443