My App wants to send Notifications, but the new Permission added in Android 33 defaults to be denied, and somehow Android does not automatically prompt the user when trying to create a Notification Channel. Is there anything I'm missing? If not, how (and when) do I request the Permission?
The Notification permission pop-up won't be shown automatically. You have to request this permission manually using the standard way to request it and handle the result.
Also, Google recommends these best practices for when and how.
Try this for request permission in sdk 33
package com.pk.name;
import android.Manifest;
import android.app.ProgressDialog;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
final int PERMISSION_REQUEST_CODE =112;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT > 32) {
if (!shouldShowRequestPermissionRationale("112")){
getNotificationPermission();
}
}
}
public void getNotificationPermission(){
try {
if (Build.VERSION.SDK_INT > 32) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.POST_NOTIFICATIONS},
PERMISSION_REQUEST_CODE);
}
}catch (Exception e){
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// allow
} else {
//deny
}
return;
}
}
}
The Notification permission pop-up won't be shown automatically. You have to request this permission manually using the standard way to request it and handle the result.
Also, Google recommends these best practices for when and how.
Declare notification permission in your Manifest file:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
Check permission:
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
fun checkNotificationPermission() {
val permission = Manifest.permission.POST_NOTIFICATIONS
when {
ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED -> {
// make your action here
}
shouldShowRequestPermissionRationale(permission) -> {
showPermissionRationaleDialog() // permission denied permanently
}
else -> {
requestNotificationPermission.launch(permission)
}
}
}
Request permission:
private val requestNotificationPermission =
registerForActivityResult(ActivityResultContracts.RequestPermission()) {isGranted->
if (isGranted) // make your action here
else "Permission denied".makeToast()
}
It also depends on the targetSdk
in use.
See below from the android notification-permission documentation. At least for newly installed apps.
The time at which the permissions dialog appears is based on your app's target SDK version:
If your app targets Android 13 or higher, your app has complete control over when the permission dialog is displayed. Use this opportunity to explain to users why the app needs this permission, encouraging them to grant it.
If your app targets 12L (API level 32) or lower, the system shows the permission dialog the first time your app starts an activity after you create a notification channel, or when your app starts an activity and then creates its first notification channel. This is usually on app startup.
Request For Multiple Permission According to All Api Including 33 . The following way, You can explain user Why the permissions are needed. If user Denied First Time, After the explain you can ask one more time, If user denied second Time, The screen goes to settings permission Screen :
public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;
private boolean checkAndRequestPermissions() {
int permissionReadExternalStorage;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
permissionReadExternalStorage = ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_MEDIA_IMAGES);
else
permissionReadExternalStorage = ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE);
int permissionWriteExtarnalStorage;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
permissionWriteExtarnalStorage = ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_MEDIA_AUDIO);
else
permissionWriteExtarnalStorage = ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
List<String> listPermissionsNeeded = new ArrayList<>();
if (permissionWriteExtarnalStorage != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
listPermissionsNeeded.add(Manifest.permission.READ_MEDIA_AUDIO);
else
listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (permissionReadExternalStorage != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
listPermissionsNeeded.add(Manifest.permission.READ_MEDIA_IMAGES);
else
listPermissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
int permissionVideoStorage = ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_MEDIA_VIDEO);
if (permissionVideoStorage != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.READ_MEDIA_VIDEO);
}
int notificationPermission = ContextCompat.checkSelfPermission(this,
Manifest.permission.POST_NOTIFICATIONS);
if (notificationPermission != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.POST_NOTIFICATIONS);
}
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[0]), REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}
@SuppressWarnings("ConstantConditions")
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions[], @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_ID_MULTIPLE_PERMISSIONS: {
Map<String, Integer> perms = new HashMap<>();
// Initialize the map with both permissions
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
perms.put(Manifest.permission.READ_MEDIA_IMAGES, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_MEDIA_AUDIO, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_MEDIA_VIDEO, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.POST_NOTIFICATIONS, PackageManager.PERMISSION_GRANTED);
}else {
perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
}
// Fill with actual results from user
if (grantResults.length > 0) {
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if ( perms.get(Manifest.permission.READ_MEDIA_IMAGES) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.READ_MEDIA_AUDIO) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.READ_MEDIA_VIDEO) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED
) {
Toast.makeText(this, "Jajakumullah, For Granting Permission.", Toast.LENGTH_LONG).show();
//else any one or both the permissions are not granted
} else {
if ( ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_MEDIA_IMAGES)
|| ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_MEDIA_AUDIO)
|| ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_MEDIA_VIDEO)
|| ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.POST_NOTIFICATIONS)
) {
showDialogOK("Necessary Permissions required for this app",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
checkAndRequestPermissions();
break;
case DialogInterface.BUTTON_NEGATIVE:
// proceed with logic by disabling the related features or quit the app.
Toast.makeText(_StartActivity_First.this, "Necessary Permissions required for this app", Toast.LENGTH_LONG).show();
// permissionSettingScreen ( );
// finish();
break;
}
}
});
}
//permission is denied (and never ask again is checked)
//shouldShowRequestPermissionRationale will return false
else {
permissionSettingScreen();
}
}
}else {
if (perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
) {
Toast.makeText(this, "Jajakumullah, For Granting Permission.", Toast.LENGTH_LONG).show();
//else any one or both the permissions are not granted
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
|| ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)
) {
showDialogOK("Necessary Permissions required for this app",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
checkAndRequestPermissions();
break;
case DialogInterface.BUTTON_NEGATIVE:
// proceed with logic by disabling the related features or quit the app.
Toast.makeText(_StartActivity_First.this, "Necessary Permissions required for this app", Toast.LENGTH_LONG).show();
// permissionSettingScreen ( );
// finish();
break;
}
}
});
}
//permission is denied (and never ask again is checked)
//shouldShowRequestPermissionRationale will return false
else {
permissionSettingScreen();
}
}
}
}
}
}
}
private void permissionSettingScreen() {
Toast.makeText(this, "Enable All permissions, Click On Permission", Toast.LENGTH_LONG)
.show();
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
// finishAffinity();
finish();
}
private void showDialogOK(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", okListener)
.create()
.show();
}
Then Call It Any Where :
if (checkAndRequestPermissions()) {
// Do your desire work here
} else {
// Call Again :
checkAndRequestPermissions();
}
Must Declare the permissions in Manifest :
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
You can request the permission for Notification in android as
with(NotificationManagerCompat.from(this)) {
if (ActivityCompat.checkSelfPermission(
this@MainActivity,
Manifest.permission.POST_NOTIFICATIONS
) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(this@MainActivity, arrayOf(Manifest.permission.POST_NOTIFICATIONS), 1)
shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)
println("check permission")
println(areNotificationsEnabled())
return@with
}
// notificationId is a unique int for each notification that you must define.
//notify(NOTIFICATION_ID, builder.build())
}
© 2022 - 2025 — McMap. All rights reserved.
shouldShowRequestPermissionRationale
being true doesn't necessarily mean the permission was permanently denied, you should still ask for it. – Amberly