简体   繁体   中英

REQUEST_IGNORE_BATTERY_OPTIMIZATIONS Firing but No Prompt

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?

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM