4

I'm trying to disable battery optimization on Android 9 with ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS in a push notification when the app is launched.

In the activity file (using notification builder):

Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 0, intent, 0);
NotificationCompat.Builder...
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getContext());
notificationManager.notify(notificationId, builder.build());

In the AndroidManifest.xml:

<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

I'm able to get the push notification to show, but when you tap, it just disappears. The prompt for 'Allow' doesn't pop up. Am I doing something wrong with the setup?

acarlstein
  • 1,799
  • 2
  • 13
  • 21
  • Check the logcat. Don't filter it or you might miss something important. You could also try changing the `Intent` to open some other app that doesn't require permissions and see if that works (just to narrow down the problem) – David Wasser Apr 17 '19 at 10:59
  • Is this still a recommended permission by Google? – IgorGanapolsky Apr 16 '20 at 13:01

1 Answers1

1

You are missing the Uri in the data. From the documentation:

Input: The Intent's data URI must specify the application package name to be shown, with the "package" scheme. That is "package:com.my.app".

Do something like:

Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parseString("package:my.package.name");
David Wasser
  • 93,459
  • 16
  • 209
  • 274