title: Android Xamarin make push notification not create a new activity but use the current one
body: <p>We're currently working on a mobile app in Xamarin Forms for iOS, Android and WP8 and I'm currently working on the notifications part for Android.</p>
  
  <p>Right now I am able to receive notifications and show them to the user and when they click on the notification it takes them to the app but it doesn't work like we want it to work. Instead of continuing on in the same Activity it starts a whole new activity which loses the entire context of the actual app.</p>
  
  <p>On our push notification receiver we are overriding the OnMessage method which get's called as soon as something comes in from our server and in here we have the following code</p>
  
  <pre><code>protected override void OnMessage(Context context, Intent intent)
      {
          string message = string.Empty;
  
          // Extract the push notification message from the intent.
          if (intent.Extras.ContainsKey("message"))
          {
              message = intent.Extras.Get("message").ToString();
              var title = "Notification:";
  
              // Create a notification manager to send the notification.
              var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
  
              Intent resultIntent = new Intent(context, typeof(MainActivity));
              resultIntent.PutExtras(intent.Extras);
  
              PendingIntent contentIntent = PendingIntent.GetActivity(
                                                context,
                                                0,
                                                new Intent(),
                                                PendingIntentFlags.UpdateCurrent
                                            );
  
  
              // Create the notification using the builder.
              var builder = new Notification.Builder(context);
              builder.SetAutoCancel(true);
              builder.SetContentTitle(title);
              builder.SetContentText(message);
              builder.SetSmallIcon(Resource.Drawable.icon);
              builder.SetContentIntent(contentIntent);
              builder.SetExtras(intent.Extras);
  
              var notification = builder.Build();
  
              // Display the notification in the Notifications Area.
              notificationManager.Notify(0, notification);
          }
      }
  </code></pre>
  
  <p>In the MainActivity.cs I am able to catch the data form the Notification when the user presses it but that creates a new activity instead of continuing on in the current one (which PendingIntentFlags.UpdateCurrent should define).</p>
  
  <p>The basics of what I want to do is basically have the same manner of receiving notifications on Android as we do on iOS where it basically just calls a delegate in the root of the application which then sends the information through to the app itself.</p>
  
  <p>I myself have little experience with Android and most of my Google searches don't show any way of executing code when the notification is pressed and also loading it's data, the just show how to create a notification without it doing much of anything.</p>
  
  <p>Edit:</p>
  
  <p>The issue has been resolved, here's how</p>
  
  <p>In the MainActivity.cs I've added LaunchMode = LaunchMode.SingleTop to the Activity attribute and overridden the method OnNewIntent like this</p>
  
  <pre><code>protected override void OnNewIntent(Intent intent)
  {
      string json = intent.Extras.GetString("payload");
      json = HttpUtility.UrlDecode(json).Replace("\\", "");
      PushReceiver.HandlePush(json, true);
  
      base.OnNewIntent(intent);
  }
  </code></pre>
  
  <p>And in my PushBroadcastReceiver I've changed the OnMessage method to </p>
  
  <pre><code>protected override void OnMessage(Context context, Intent intent)
      {
          string message = string.Empty;
  
          // Extract the push notification message from the intent.
          if (intent.Extras.ContainsKey("message"))
          {
              message = intent.Extras.Get("message").ToString();
              var title = "Notification:";
  
              // Create a notification manager to send the notification.
              var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
  
              Intent resultIntent = new Intent(context, typeof(MainActivity));
              resultIntent.PutExtras(intent.Extras);
  
              PendingIntent resultPendingIntent =
                  PendingIntent.GetActivity(
                      context,
                      0,
                      resultIntent,
                      PendingIntentFlags.UpdateCurrent
                  );
  
              // Create the notification using the builder.
              var builder = new Notification.Builder(context);
              builder.SetAutoCancel(true);
              builder.SetContentTitle(title);
              builder.SetContentText(message);
              builder.SetSmallIcon(Resource.Drawable.icon);
              builder.SetContentIntent(resultPendingIntent);
  
              var notification = builder.Build();
  
              // Display the notification in the Notifications Area.
              notificationManager.Notify(new Random((int)(DateTime.Now.ToFileTime() % int.MaxValue)).Next(), notification);
  
          }
      }
  </code></pre>
  
  <p>Because of the 'LaunchMode = LaunchMode.SingleTop' and 'PendingIntentFlags.UpdateCurrent' the MainActivity no longer get's re-created but the OnNewIntent event is called every time the user clicks on the notification, when the OnNewIntent event is caught you have full access to the app through App.Current (and cast it to the necessary Page/View/Class) because no new Activity is created, it also ensures that the Notification works without hitches that would be caused by re-creating the Activity.</p>
  
  <p>Thanks <a href="https://stackoverflow.com/users/1048571/jon-douglas">Jon Douglas</a>!</p>