androidpush-notificationintentserviceandroid-workmanagerandroid-12

Start activity from IntentService does not work in Android 12


Quick background: I have updated my latest targetSdkVersion to the latest Android 31

I have a service class that gets executed after a notification is clicked from the notification tray, it contains below code:

public class NotificationClickListener extends IntentService {

    @Override
    protected void onHandleIntent(Intent intent) {
        Intent myIntentFromPendingIntent = intent;
        //insert some necessary code

        myIntentFromPendingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getApplication().startActivity(myIntentFromPendingIntent); //this line here gets executed but the application or the activity wasn't opened after this line
    }

The PendingIntent is created like the below code after a message is received

    PendingIntent contentIntent = PendingIntent.getService(appContext, 0, intent, PendingIntent.FLAG_IMMUTABLE);
    mNotificationManager.notify(0, myNotification);

I am not getting any error logs or any relevant trace logs, the only logs I am seeing is

    D/skia: --- Failed to create image decoder with message 'unimplemented'

I have browsed some documentation and have stumbled upon Restrictions on starting activities from the background, and there were some listed exceptions, one of which is that when "The app has an activity in the back stack of the foreground task." So I was expecting that my activity would be started (given that my application was paused in the background)


Can anyone help me locate the issue? Is it possible to still fix this using IntentService?

Or is it now necessary to migrate using WorkManager? If so, can you also please leave a good reference to check out.

Thanks very much.


Solution

  • So I was expecting that my activity would be started (given that my application was paused in the background)

    FWIW it appears that it does not qualify for the rule that you cited: "The app has an activity in the back stack of the foreground task." (emphasis added) — your app is in the background, not the foreground.

    I have a service class that gets executed after a notification is clicked from the notification tray, it contains below code:

    Have your notification launch the activity directly. "Trampolines" like this are banned once your targetSdkVersion reaches 31 or higher.