Push Notifications Lifecycle

Push notifications are a common feature in mobile apps and if you read my previous post or others you will easily understand how to receive them. Most walk throughs stop at that point but what do you do after you receive them? As you will find out there are a few scenario’s in different platforms you need to accommodate.

push notifications


Android is the most interesting to handle push notifications, where its handy to have a few tricks up your sleeve. When you are in the foreground in your class which inherited GcmServiceBase, there is an OnMessageReceived method which is called when a push notification is received. When your app is backgrounded it will come through the MainActivity.cs.


In the foreground, you will need to code yourself or use a plugin like Toast Notifications for Xamarin and Windows to show a Snackbar or Notification. You must handle the display of the push notification yourself. When backgrounded, the user opens the app via clicking the notification, you must handle the response in the MainActivity, but while most examples might show you using it in the OnCreate, this actually isn’t a great place to use it.

In your MainActivity.cs set your LaunchMode to SingleTop. This means your MainActivity wont be created again when opened.

[Activity(Label = "Mobile App", Theme = "@style/MainTheme", LaunchMode = LaunchMode.SingleTop)]

Next use the OnNewIntent to handle the notification arguments passed through, instead of OnCreate.

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

        if (!string.IsNullOrEmpty(message))
              // Do something


In iOS, if the app is in the foreground, a different method is called than when in the app is in the background. ReceivedRemoteNotification is used when the app is backgrounded, DidReceiveRemoteNotification is called when the app is in the foreground.


If the app is in the foreground, it also won’t show a push notification, you have to manually either show an alert or show a notification of your own. For this you might want to look at the Toast Notifications for Xamarin and Windows plugin, or manually send an UNNotificationRequest (iOS 10+) if you want to roll your own.


Windows is the easiest platform, as a Toast Notification will be displayed if the app is in the foreground and when the app is backgrounded. When a user clicks on the Toast, the OnActivated function will be called in your App.xaml.cs.


The args in here are from the launch argument in your toast if you placed any in the notification you sent.

Microsoft MVP (Xamarin) | Exrin | Xamarin Forms Developer | Melbourne, Australia | Open to sponsorship to Canada or US

Related Posts


  1. Emanuel Nogueiras

    Hi, I am new in xamarin. I have a portable project with android push notifications workin

    But…. when I receive a push in the android project (onMessage) I need to call a void in portable project… and I do not know how to access to it…

    I think of use events or delegates but really I do not know what they are or how to use it

    Do you think you can help me in some way?

  2. Shahriat Hossain

    I am using args as extra like below:

    var jGcmData = new JObject();
    var jData = new JObject();

    jData.Add(“message”, MESSAGE);
    jData.Add(“args”, “1”);
    jGcmData.Add(“to”, “/topics/pushString”);
    jGcmData.Add(“data”, jData);

    and trying to receive using your given code below on MainActivity:

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

    if (!string.IsNullOrEmpty(args))
    // Do something

    but here getting args (null)
    can you help me what I am doing wrong?

Leave A Comment?