diff --git a/res/values/strings.xml b/res/values/strings.xml
index f2e7f6015..3b7921e21 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -482,6 +482,8 @@
Default color
Custom image
Custom color
+ Fast notifications in background
+ Uses a background connection to your server and requires ignored battery optimizations.
Reliable background connection
Requires a permanent notification
diff --git a/res/xml/preferences_notifications.xml b/res/xml/preferences_notifications.xml
index 3971ff3bb..113418b22 100644
--- a/res/xml/preferences_notifications.xml
+++ b/res/xml/preferences_notifications.xml
@@ -7,6 +7,13 @@
android:title="@string/pref_notifications"
android:defaultValue="true" />
+
+
{
+ requestToggleIgnoreBatteryOptimizations();
+ return true;
+ });
+
CheckBoxPreference reliableService = this.findPreference("pref_reliable_service");
reliableService.setOnPreferenceChangeListener((preference, newValue) -> {
@@ -96,6 +110,9 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme
public void onResume() {
super.onResume();
((ApplicationPreferencesActivity) getActivity()).getSupportActionBar().setTitle(R.string.pref_notifications);
+
+ // upate ignoreBattery in onResume() to reflects changes done in the system settings
+ ignoreBattery.setChecked(isIgnoringBatteryOptimizations());
}
@Override
@@ -133,6 +150,45 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme
}
}
+ private boolean needsIgnoreBatteryOptimizations() {
+ return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
+ }
+
+ private boolean isIgnoringBatteryOptimizations() {
+ if (!needsIgnoreBatteryOptimizations()) {
+ return true;
+ }
+ PowerManager pm = (PowerManager)getActivity().getSystemService(Context.POWER_SERVICE);
+ if(pm.isIgnoringBatteryOptimizations(getActivity().getPackageName())) {
+ return true;
+ }
+ return false;
+ }
+
+ private void requestToggleIgnoreBatteryOptimizations() {
+ Context context = getActivity();
+ boolean openManualSettings = true;
+
+ try {
+ if (needsIgnoreBatteryOptimizations()
+ && !isIgnoringBatteryOptimizations()
+ && ContextCompat.checkSelfPermission(context, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) == PackageManager.PERMISSION_GRANTED) {
+ Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, Uri.parse("package:" + context.getPackageName()));
+ context.startActivity(intent);
+ openManualSettings = false;
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ if (openManualSettings && needsIgnoreBatteryOptimizations()) {
+ // fire ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS if ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS fails
+ // or if isIgnoringBatteryOptimizations() is already true (there is no intent to re-enable battery optimizations)
+ Intent intent = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
+ context.startActivity(intent);
+ }
+ }
+
private void initializeRingtoneSummary(Preference pref) {
RingtoneSummaryListener listener = (RingtoneSummaryListener) pref.getOnPreferenceChangeListener();
Uri uri = Prefs.getNotificationRingtone(getContext());