Skip to content

Commit

Permalink
Request permission to send notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Stypox committed Nov 28, 2022
1 parent f07886f commit 9d01d88
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
12 changes: 9 additions & 3 deletions app/src/main/java/org/schabi/newpipe/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,12 @@ protected void onCreate(final Bundle savedInstanceState) {
}
openMiniPlayerUponPlayerStarted();

// Schedule worker for checking for new streams and creating corresponding notifications
// if this is enabled by the user.
NotificationWorker.initialize(this);
if (PermissionHelper.checkPostNotificationsPermission(this,
PermissionHelper.POST_NOTIFICATIONS_REQUEST_CODE)) {
// Schedule worker for checking for new streams and creating corresponding notifications
// if this is enabled by the user.
NotificationWorker.initialize(this);
}
}

@Override
Expand Down Expand Up @@ -599,6 +602,9 @@ public void onRequestPermissionsResult(final int requestCode,
((VideoDetailFragment) fragment).openDownloadDialog();
}
break;
case PermissionHelper.POST_NOTIFICATIONS_REQUEST_CODE:
NotificationWorker.initialize(this);
break;
}
}

Expand Down
16 changes: 14 additions & 2 deletions app/src/main/java/org/schabi/newpipe/util/PermissionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.schabi.newpipe.settings.NewPipeSettings;

public final class PermissionHelper {
public static final int POST_NOTIFICATIONS_REQUEST_CODE = 779;
public static final int DOWNLOAD_DIALOG_REQUEST_CODE = 778;
public static final int DOWNLOADS_REQUEST_CODE = 777;

Expand Down Expand Up @@ -71,8 +72,7 @@ public static boolean checkWriteStoragePermissions(final Activity activity,

// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
requestCode);
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, requestCode);

// PERMISSION_WRITE_STORAGE is an
// app-defined int constant. The callback method gets the
Expand All @@ -83,6 +83,18 @@ public static boolean checkWriteStoragePermissions(final Activity activity,
return true;
}

public static boolean checkPostNotificationsPermission(final Activity activity,
final int requestCode) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU
&& ContextCompat.checkSelfPermission(activity,
Manifest.permission.POST_NOTIFICATIONS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity,
new String[] {Manifest.permission.POST_NOTIFICATIONS}, requestCode);
return false;
}
return true;
}

/**
* In order to be able to draw over other apps,
Expand Down

0 comments on commit 9d01d88

Please sign in to comment.