Recently, we have upgraded the SDK version of our app to 34 (Android 14) and after the we have noticed that our app is getting launched when the user clicks on the Notification.
Our initial research suggested that there are few restrictions on starting activities from background for Android 14.
We did go through the documentation and tried adding ActivityOptions.setPendingIntentBackgroundActivityStartMode(ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED) to the pending intent that is responsible for starting the main activity on the notification click but this did not work out for us for some reason.
Secondly, we tried asking the user for the “SYSTEM_ALERT_WINDOW” permission but realised that this permission needs to be explicitly given by the user from the settings thereby, hindering our user experience.
Therefore, I wanted to understand the following things:
What could have possibly gone wrong with the first MODE_BACKGROUND_ACTIVITY_START_ALLOWED?
Why has Android restricted BAL?
Is there another way to programatically grant the “SYSTEM_ALERT_WINDOW” permission after the user’s consent instead of redirecting them to the settings?
Finally, is there any other solution that could resolve the issue that we are facing currently?
Code snippet:
public static Bundle getActivityOptions() {
ActivityOptions options = ActivityOptions.makeBasic();
options.setPendingIntentBackgroundActivityStartMode(ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED);
return options.toBundle();
}
Intent notifyIntent = new Intent();
notifyIntent.setClass(context, MainActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
pendingIntent = PendingIntent.getActivity(
context,
requestCode,
notifyIntent,
PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
);
try {
pendingIntent.send(getActivityOptions());
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
Fix this issue. Bypass the BAL block.
Poojitha Chivukula is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1