Push Notifications in Xamarin Forms

Updated: 2017-02-11

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.

push notifications

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.


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.


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.


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


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.


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);


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.


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");


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


  1. Kornelis

    Looking to implement push notifications for our project (using Xamarin Forms). Stumbled upon this great post: thanks a lot!

  2. Olamide James

    Hello Thanks a lot for this tutorial, I just created my first Xamarin.Android push notification project which works well. But i have these questions:

    1. Can mobile devices ( if connected to the internet) receive push notifications even if the app is currently not running.

    2. Is it possible to open up a particular page from my app when i click on the notification message


  3. Lucas

    Hi Adam, thanks a lot for the info.

    I’m facing an issue: when my app is not running, notifications aren’t displayed.
    I’ve seen many posts on Xamarin forum, but none is clear enough.

    Can you help me please?


      1. Lucas

        Yes, I’m referring to Android.
        I appreciate if you can update it, I’ll be looking forward to it.

        Thank you!

  4. Shahriat Hossain

    Hi Adam,
    I can publish message to topic from my xamarin pcl code but I need to subscribe the topic from android or ios’s own platform because I need to pass device token from those specific platform but I want to subscribe also from my pcl project, can I do this feature?

    If so can you give me some article where I can get the idea how to then generate device token from pcl project?

    1. Adam Pedley

      Yes you can send a Push Notification to a specific device. When you follow the Push Notification guides by Microsoft, you will notice, when you register to the Azure Notification Hub, you can pass through tags.

      These tags can be anything such as an email address., device ID, userId etc. Then when you send a push notification, you send it to a particular tag, which is tied to only one device, or one user, depending on how you set it up.

  5. Baz Templer

    Great post. Your last comment about making Android LaunchMode = LaunchMode.SingleTop just made my day. I was trying all sorts of interesting ways work around the application recreating the main activity in Android. Thank you!!