Friday 19 May 2023

Start Activity is not working in Android 13

Description I am using the Firebase Messaging service for handling Push notifications On notification tapped Intent OnHandleIntent method calls I tried to start the activity of Splash screen it works for Android 10 but not for Android 13 devices.

I do receive the notification in all modes whether the app is in foreground, background or killed mode.

But when the app is in the background or killed mode and I tapped the notification it did not do anything. I debug it into background mode and on the Tapped notification it reached onHandleIntent method where I create a new intent of splash screen and start the activity but the activity did not run app did not come into the foreground or call the splash activity code

Steps to Reproduce

  1. Create a Notification Intent
  2. On Notification Tapped OnHandleIntent try to start any activity whether the splash activity or Main activity It did not run for Android 13 devices

Basic Information

  • Android:
  • Affected Devices: Android 13 devices

Code Sample

Notification Activity

public class NotificationIntentService : IntentService
{
    public NotificationIntentService() : base("NotificationIntentService")
    {

    }

    protected override async void OnHandleIntent(Intent intent)
    {
        try
        {

            var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
            notificationManager.Cancel(0);

            var action = intent.Extras.GetString("action"); 

            if (string.IsNullOrWhiteSpace(action))
            {
                action = "default";
            }

            var newIntent = new Intent(this, typeof(SplashActivity));

            if (intent?.Extras != null)
            {
                newIntent.PutExtras(intent.Extras);
            }

            newIntent.AddFlags(ActivityFlags.NewTask);
            StartActivity(newIntent);

        }
        catch (System.Exception ex)
        {
           
        }
    }

    
}

Splash Activity

[Activity(Theme = "@style/MyTheme.Splash", MainLauncher = true, NoHistory = true)]
public class SplashActivity : AppCompatActivity
{
    static readonly string TAG = "X:" + typeof(SplashActivity).Name;

    public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
    {
        base.OnCreate(savedInstanceState, persistentState);
        
    }

    protected override void OnResume()
    {
        base.OnResume();
        var mainActivity = new Android.Content.Intent(Application.Context, typeof(MainActivity));
        if (Intent?.Extras != null)
        {
            mainActivity.PutExtras(Intent.Extras);
        }


        StartActivity(mainActivity);
    }

    public override void OnBackPressed() { }
}

Expected Behavior

It should run the splash activity which will run the main activity, Same code worked fine for Android 10 devices but not on Android 13

Actual Behavior

OnHandleIntent Start Activity did not run any activity for Android 13 devices

Code Sample

here is the sample to recreate the issue

https://github.com/aakashsolangi/SampleNotification



from Start Activity is not working in Android 13

No comments:

Post a Comment