1
0
Fork 0
mirror of https://github.com/TeamNewPipe/NewPipe.git synced 2025-10-03 09:49:21 +02:00

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.
This commit is contained in:
VougJo23 2025-04-24 02:01:56 +03:00 committed by Profpatsch
parent 42a52b7118
commit d63db94477
3 changed files with 73 additions and 16 deletions

View file

@ -179,12 +179,17 @@ public class MainActivity extends AppCompatActivity {
} }
openMiniPlayerUponPlayerStarted(); openMiniPlayerUponPlayerStarted();
if (PermissionHelper.checkPostNotificationsPermission(this, PermissionHelper.checkPostNotificationsPermissionOnStartup(
PermissionHelper.POST_NOTIFICATIONS_REQUEST_CODE)) { this,
// Schedule worker for checking for new streams and creating corresponding notifications notificationAllowed -> {
// if this is enabled by the user. // Schedule worker for checking for new streams and creating corresponding
NotificationWorker.initialize(this); // notifications if this is enabled by the user.
} if (Boolean.TRUE.equals(notificationAllowed)) {
NotificationWorker.initialize(this);
}
}
);
if (!UpdateSettingsFragment.wasUserAskedForConsent(this) if (!UpdateSettingsFragment.wasUserAskedForConsent(this)
&& !App.getApp().isFirstRun() && !App.getApp().isFirstRun()
&& ReleaseVersionUtil.INSTANCE.isReleaseApk()) { && ReleaseVersionUtil.INSTANCE.isReleaseApk()) {

View file

@ -1,23 +1,32 @@
package org.schabi.newpipe.util; package org.schabi.newpipe.util;
import android.Manifest; import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.content.ActivityNotFoundException; import android.content.ActivityNotFoundException;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.provider.Settings; import android.provider.Settings;
import android.widget.Toast; import android.widget.Toast;
import androidx.activity.ComponentActivity;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.preference.PreferenceManager;
import org.schabi.newpipe.R; import org.schabi.newpipe.R;
import org.schabi.newpipe.settings.NewPipeSettings; import org.schabi.newpipe.settings.NewPipeSettings;
import java.util.function.Consumer;
public final class PermissionHelper { public final class PermissionHelper {
public static final int POST_NOTIFICATIONS_REQUEST_CODE = 779; public static final int POST_NOTIFICATIONS_REQUEST_CODE = 779;
public static final int DOWNLOAD_DIALOG_REQUEST_CODE = 778; public static final int DOWNLOAD_DIALOG_REQUEST_CODE = 778;
@ -81,17 +90,59 @@ public final class PermissionHelper {
return true; return true;
} }
public static boolean checkPostNotificationsPermission(final Activity activity,
final int requestCode) { /**
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU * Check that we have the notification permission or ask the user for permission.
&& ContextCompat.checkSelfPermission(activity, *
Manifest.permission.POST_NOTIFICATIONS) * @param activity main activity
!= PackageManager.PERMISSION_GRANTED) { * @param userChoice a callback that gets called with the user choice
ActivityCompat.requestPermissions(activity, */
new String[] {Manifest.permission.POST_NOTIFICATIONS}, requestCode); public static void checkPostNotificationsPermissionOnStartup(
return false; final ComponentActivity activity,
final Consumer<Boolean> userChoice) {
// On Android before TIRAMISU, notifications are always allowed to be sent
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
userChoice.accept(true);
return;
} }
return true;
// if we have the permission already, continue
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.POST_NOTIFICATIONS)
== PackageManager.PERMISSION_GRANTED) {
userChoice.accept(true);
return;
}
// if we already asked the user, dont ask again
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
final String wasAskedKey = activity.getString(
R.string.user_was_asked_notification_permission_on_startup_key);
if (prefs.getBoolean(wasAskedKey, false)) {
userChoice.accept(false);
return;
}
// else lets ask the user for permission
@SuppressLint("ApplySharedPref")
final ActivityResultCallback<Boolean> cb = isGranted -> {
// first make sure that we only ever ask the user once on startup
final SharedPreferences prefs2 =
PreferenceManager.getDefaultSharedPreferences(activity);
// commit setting before doing anything else
prefs2.edit().putBoolean(wasAskedKey, true).commit();
// forward the user choice
userChoice.accept(isGranted);
};
final ActivityResultLauncher<String> notificationRequestReference =
activity.registerForActivityResult(
new ActivityResultContracts.RequestPermission(),
cb
);
notificationRequestReference.launch(Manifest.permission.POST_NOTIFICATIONS);
} }
/** /**

View file

@ -1405,6 +1405,7 @@
<string name="recaptcha_cookies_key">recaptcha_cookies_key</string> <string name="recaptcha_cookies_key">recaptcha_cookies_key</string>
<string name="user_was_asked_notification_permission_on_startup_key">user_was_asked_notification_permission_on_startup</string>
<string name="enable_streams_notifications">enable_streams_notifications</string> <string name="enable_streams_notifications">enable_streams_notifications</string>
<string name="streams_notifications_interval_key">streams_notifications_interval</string> <string name="streams_notifications_interval_key">streams_notifications_interval</string>
<string name="streams_notifications_interval_default">14400</string> <string name="streams_notifications_interval_default">14400</string>