1

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 ?

Ophelie
  • 41
  • 5
  • 1
    What is your Target Android version? Is it 12? If so then are you sure your app is getting a chance to Request the notification channel permission? If it is 13 you can handle it yourself https://developer.android.com/develop/ui/views/notifications/notification-permission#wait-to-show-prompt – FreakyAli Oct 06 '22 at 16:20
  • You can try to start an activity with the pending intent according to the [official document](https://developer.android.com/develop/ui/views/notifications/navigation#DirectEntry). – Liyun Zhang - MSFT Oct 07 '22 at 09:59
  • @FreakyAli I target Android 13 and ask myself for the permission, also I always check if I have it before sending the notification so it should not be a problem of permissions. – Ophelie Oct 10 '22 at 07:35
  • @LiyunZhang-MSFT Actually my Notification project is only a small project I reference in my bigger project. so I don't have access to the activity I would like to start. Also as I said before, the previous versions of Android can set the App to the foreground without having an activity in the Intents. – Ophelie Oct 10 '22 at 07:35
  • Does this `packageManager.GetLaunchIntentForPackage` return null when you run it on the android 12? – Liyun Zhang - MSFT Oct 10 '22 at 09:13
  • Nope, it does create the intent – Ophelie Oct 10 '22 at 12:44

1 Answers1

3

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

Ophelie
  • 41
  • 5