Home > database >  Bring app back to foreground from code doesn't work on Android 12
Bring app back to foreground from code doesn't work on Android 12

Time:10-13

I made a Xamarin.Forms project to create and show local notifications, and it's supposed to be able to put the app back to the foreground when the notification is clicked.

The thing is, my code works on Android 11 and before, but on Android 12 & 13 the notification click is received by the app, if I have a callback for that notification it is called, but the app stays in background.

This is the part of the code that runs when I received a notification click and that I want to set the app in foreground (this is in the Xamarin Android project) :

var packageManager = Application.Context.PackageManager;
Intent launchIntent = packageManager.GetLaunchIntentForPackage(Constants.PackageName);

if (launchIntent != null)
{
    launchIntent.AddCategory(Intent.CategoryLauncher);
    Application.Context.StartActivity(launchIntent);
}

I have found a lot of posts on how to start/set to foreground an app, and the code I use is what's working for others, but all these posts where from 2020 and before, so no Android 12 at the time and I can't find anything about a new way of doing this.

Does anyone have this functionality working on the newest Androids ?

CodePudding user response:

I have found the solution so I'll post it here if someone needs it.

The code I use to put the application back to the foreground is correct, but I was missing the System_Alert_Window permission in my main application.

So to handle this permission I did :

  • Add it to my main application's manifest
  • Create a native method that checks if it is enabled
  • Create a native method that redirect the user to the overlay settings so they can allow the permission.

To check if the permission is enabled :

public bool HasOverlayPermission()
{
    if (Build.VERSION.SdkInt < BuildVersionCodes.M)
        return true;

    return Android.Provider.Settings.CanDrawOverlays(Application.Context);
}

To redirect the user to their phone settings for AppOverlay (this permission can only be allowed from the settings) :

public void AskForOverlayPermission()
{
    if (Android.Provider.Settings.CanDrawOverlays(Application.Context))
        return;
    var uri = Android.Net.Uri.Parse("package:"   
        Application.Context.PackageName);
    Intent intent = new Intent(
        Android.Provider.Settings.ActionManageOverlayPermission, uri);
    _mainActivity.StartActivityForResult(intent, 101);
}

The StartActivityFromResult method is only accessible in Activity classes, so you can either write it in you MainActivity or give your MainActivity as constructor parameter of another class.

This code will directly redirect the user to the settings page, so it's better if you ask them if they want to allow this permission in a popup or something beforehand (so they can understand why they're redirected).

I have found the code in this post : How to enable screen overlay permission by default

  • Related