skip to Main Content

In manifest:

<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

In Activity:

if (Build.VERSION.SDK_INT >= 33) {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.POST_NOTIFICATIONS},1);
        }
        else {
            //
        }
    }

In the build.gradle file the targetSdkVersion is 34

I have other permission requests like Camera, Location, and Microphone, and all of these work fine, but the POST NOTIFICATIONS request doesn’t. The dialog box for asking the user to allow it doesn’t appear.

The notifications work only if I activate them manually in Settings -> App -> Notifications -> Allow Notifications

Don’t know what is the problem! Ohh and I know about this android developer resource

2

Answers


  1. Chosen as BEST ANSWER

    I made it work by grouping the Manifest.permission.POST_NOTIFICATIONS with the others:

        private void checkAllPermissions() {
        int Permission_All = 1;
        String[] Permissions = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, 
                Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, 
                Manifest.permission.RECORD_AUDIO, Manifest.permission.INTERNET, Manifest.permission.FOREGROUND_SERVICE,
                Manifest.permission.WAKE_LOCK, Manifest.permission.DISABLE_KEYGUARD, 
                Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.POST_NOTIFICATIONS};
        if (!hasPermissions(this, Permissions)) {
            ActivityCompat.requestPermissions(this,Permissions, Permission_All);
        }
    }
    
        public static boolean hasPermissions(Context context, String... permissions) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
            for (String permission : permissions) {
                if (ActivityCompat.checkSelfPermission(context,permission) != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
        }
        return true;
    }
    

    The code above works!! But don't know why it didn't, in the previous stand-alone code. I even added toast and log lines to check if the IF condition was met, and all seemed OK except it didn't ask for the POST_NOTIFICATION permission.


  2. I tried your code it is working fine for me:

    Few things you can re-check:

    1. Make sure you are using emulator/device above 33
    2. Your Manifest should be android.Manifest

    Also more you can check in this answer

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search