Skip to content

Commit d63db94

Browse files
VougJo23Profpatsch
authored andcommitted
fix: only ask for Notification permission once on first startup
This seems to be the only reliable way of making sure we only ever bother the user once. If they accept, from then on the permission check will be successful. If they deny or swipe away, we never ask again at startup and the user has to manually change it in the settings or go to our settings and re-do the prompt by activating notifications.
1 parent 42a52b7 commit d63db94

File tree

3 files changed

+73
-16
lines changed

3 files changed

+73
-16
lines changed

app/src/main/java/org/schabi/newpipe/MainActivity.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,17 @@ protected void onCreate(final Bundle savedInstanceState) {
179179
}
180180
openMiniPlayerUponPlayerStarted();
181181

182-
if (PermissionHelper.checkPostNotificationsPermission(this,
183-
PermissionHelper.POST_NOTIFICATIONS_REQUEST_CODE)) {
184-
// Schedule worker for checking for new streams and creating corresponding notifications
185-
// if this is enabled by the user.
186-
NotificationWorker.initialize(this);
187-
}
182+
PermissionHelper.checkPostNotificationsPermissionOnStartup(
183+
this,
184+
notificationAllowed -> {
185+
// Schedule worker for checking for new streams and creating corresponding
186+
// notifications if this is enabled by the user.
187+
if (Boolean.TRUE.equals(notificationAllowed)) {
188+
NotificationWorker.initialize(this);
189+
}
190+
}
191+
);
192+
188193
if (!UpdateSettingsFragment.wasUserAskedForConsent(this)
189194
&& !App.getApp().isFirstRun()
190195
&& ReleaseVersionUtil.INSTANCE.isReleaseApk()) {

app/src/main/java/org/schabi/newpipe/util/PermissionHelper.java

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
package org.schabi.newpipe.util;
22

33
import android.Manifest;
4+
import android.annotation.SuppressLint;
45
import android.app.Activity;
56
import android.content.ActivityNotFoundException;
67
import android.content.Context;
78
import android.content.Intent;
9+
import android.content.SharedPreferences;
810
import android.content.pm.PackageManager;
911
import android.net.Uri;
1012
import android.os.Build;
1113
import android.provider.Settings;
1214
import android.widget.Toast;
1315

16+
import androidx.activity.ComponentActivity;
17+
import androidx.activity.result.ActivityResultCallback;
18+
import androidx.activity.result.ActivityResultLauncher;
19+
import androidx.activity.result.contract.ActivityResultContracts;
1420
import androidx.annotation.RequiresApi;
1521
import androidx.core.app.ActivityCompat;
1622
import androidx.core.content.ContextCompat;
23+
import androidx.preference.PreferenceManager;
1724

1825
import org.schabi.newpipe.R;
1926
import org.schabi.newpipe.settings.NewPipeSettings;
2027

28+
import java.util.function.Consumer;
29+
2130
public final class PermissionHelper {
2231
public static final int POST_NOTIFICATIONS_REQUEST_CODE = 779;
2332
public static final int DOWNLOAD_DIALOG_REQUEST_CODE = 778;
@@ -81,17 +90,59 @@ public static boolean checkWriteStoragePermissions(final Activity activity,
8190
return true;
8291
}
8392

84-
public static boolean checkPostNotificationsPermission(final Activity activity,
85-
final int requestCode) {
86-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU
87-
&& ContextCompat.checkSelfPermission(activity,
88-
Manifest.permission.POST_NOTIFICATIONS)
89-
!= PackageManager.PERMISSION_GRANTED) {
90-
ActivityCompat.requestPermissions(activity,
91-
new String[] {Manifest.permission.POST_NOTIFICATIONS}, requestCode);
92-
return false;
93+
94+
/**
95+
* Check that we have the notification permission or ask the user for permission.
96+
*
97+
* @param activity main activity
98+
* @param userChoice a callback that gets called with the user choice
99+
*/
100+
public static void checkPostNotificationsPermissionOnStartup(
101+
final ComponentActivity activity,
102+
final Consumer<Boolean> userChoice) {
103+
104+
// On Android before TIRAMISU, notifications are always allowed to be sent
105+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
106+
userChoice.accept(true);
107+
return;
93108
}
94-
return true;
109+
110+
// if we have the permission already, continue
111+
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.POST_NOTIFICATIONS)
112+
== PackageManager.PERMISSION_GRANTED) {
113+
userChoice.accept(true);
114+
return;
115+
}
116+
117+
// if we already asked the user, don’t ask again
118+
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
119+
final String wasAskedKey = activity.getString(
120+
R.string.user_was_asked_notification_permission_on_startup_key);
121+
if (prefs.getBoolean(wasAskedKey, false)) {
122+
userChoice.accept(false);
123+
return;
124+
}
125+
126+
// else let’s ask the user for permission
127+
@SuppressLint("ApplySharedPref")
128+
final ActivityResultCallback<Boolean> cb = isGranted -> {
129+
// first make sure that we only ever ask the user once on startup
130+
final SharedPreferences prefs2 =
131+
PreferenceManager.getDefaultSharedPreferences(activity);
132+
// commit setting before doing anything else
133+
prefs2.edit().putBoolean(wasAskedKey, true).commit();
134+
135+
// forward the user choice
136+
userChoice.accept(isGranted);
137+
};
138+
139+
final ActivityResultLauncher<String> notificationRequestReference =
140+
activity.registerForActivityResult(
141+
new ActivityResultContracts.RequestPermission(),
142+
cb
143+
);
144+
145+
notificationRequestReference.launch(Manifest.permission.POST_NOTIFICATIONS);
95146
}
96147

97148
/**

app/src/main/res/values/settings_keys.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,7 @@
14051405

14061406
<string name="recaptcha_cookies_key">recaptcha_cookies_key</string>
14071407

1408+
<string name="user_was_asked_notification_permission_on_startup_key">user_was_asked_notification_permission_on_startup</string>
14081409
<string name="enable_streams_notifications">enable_streams_notifications</string>
14091410
<string name="streams_notifications_interval_key">streams_notifications_interval</string>
14101411
<string name="streams_notifications_interval_default">14400</string>

0 commit comments

Comments
 (0)