Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Xamarin make push notification not create a new activity but use the current one

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.

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.

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

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

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

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.

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.

Edit:

The issue has been resolved, here's how

In the MainActivity.cs I've added LaunchMode = LaunchMode.SingleTop to the Activity attribute and overridden the method OnNewIntent like this

protected override void OnNewIntent(Intent intent)
{
    string json = intent.Extras.GetString("payload");
    json = HttpUtility.UrlDecode(json).Replace("\\", "");
    PushReceiver.HandlePush(json, true);

    base.OnNewIntent(intent);
}

And in my PushBroadcastReceiver I've changed the OnMessage method to

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

        }
    }

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.

Thanks Jon Douglas!

like image 726
MegaMiley Avatar asked Mar 14 '23 15:03

MegaMiley


1 Answers

I believe you need to make use of singleTop in your Activity here:

android:launchMode="singleTop"

http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

Additionally these Intent flags may help as well:

http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP

http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_SINGLE_TOP

like image 187
Jon Douglas Avatar answered Apr 08 '23 15:04

Jon Douglas