18

I've looked through this guide for android 13 push notifications

https://developer.android.com/about/versions/13/changes/notification-permission#user-choice

And I've looked at the guide for requesting permissions

https://developer.android.com/training/permissions/requesting#java

I've updated my compile and target to api 32.

Here is my code so far (in progress). Right now I'm just trying to get the notification prompt to show up.

        if (Build.VERSION.SDK_INT >= 32) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NOTIFICATION_POLICY) == PackageManager.PERMISSION_GRANTED)
                return;
            ActivityResultLauncher<String> launcher = registerForActivityResult(
                    new ActivityResultContracts.RequestPermission(), isGranted -> {

                    }
            );
            launcher.launch(Manifest.permission.POST_NOTIFICATIONS);
        }

The problem I have is I get an error cannot find symbol variable POST_NOTIFICATIONS.

What is the proper manifest permission for push notifications?

6
  • 1
    "I've updated my compile and target to api 32." -- are you sure that you updated compileSdkVersion to 32? That is when POST_NOTIFICATIONS was added, so your behavior would be as if your compileSdkVersion was still something lower. Commented May 19, 2022 at 19:57
  • 1
    Oh, actually, it looks like POST_NOTICATIONS is added in 33. I didn't realize 32 was android 12L. That must be my problem Commented May 19, 2022 at 20:03
  • 2
    Oh, right, I keep forgetting that 12L was 32. I need to get that tattooed to the insides of my eyelids or something. Commented May 19, 2022 at 20:27
  • 1
    Did you solve your problem? I am facing a similar issue. Commented Sep 28, 2022 at 18:38
  • @IgorGanapolsky When I originally asked the question, api 33 wasn't released yet, so that was an issue for me. But it is released now, so just change your target and compile sdk versions to 33 Commented Sep 29, 2022 at 1:49

7 Answers 7

16

You need to follow few steps, add post notifications permission in manifest

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

then in your controller as for run time permission like generally we ask:

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

Then Need to handle permission result as usual

Sign up to request clarification or add additional context in comments.

2 Comments

I just upgraded from SDK 32 to 33. My notifications went missing. This was the best answer I could find!
if this is not working add change Manifest.permission to android.Manifest.permission
11

Maybe I'm a bit late to the party, I know... But I hope this can help others at least. You need to use compileSdkVersion 33 in your gradle file at the Module level. Then you'll be allowed to use the POST_NOTIFICATIONS permission without any issue. Gradle settings

1 Comment

Doing this alone, won't still allow the user to receive push notifications, you need to request the permission as per @Manish Arora's answer
6

How to add run time permission for notification permission in android studio, Apps targeting Android 13 will now need to request notification permission from the user before posting notifications,” Behavior changes: Apps targeting Android 13 or higher https://developer.android.com/about/versions/13/behavior-changes-13

Add on manifest: uses-permission android: name="android.permission.POST_NOTIFICATIONS

and

Add in MainActivity after onCreate:

if (ContextCompat.checkSelfPermission(this, POST_NOTIFICATIONS) == PackageManager.PERMISSION_DENIED) {
            ActivityCompat.requestPermissions(this, new String[]{POST_NOTIFICATIONS}, 1);
        }

Comments

4

Android 13 (API level 33) and higher supports a runtime permission for sending non-exempt (including Foreground Services (FGS)) notifications from an app: POST_NOTIFICATIONS. This change helps users focus on the notifications that are most important to them.

reference here

So you need to add this permission to AndroidManifest.xml

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

Comments

1

Try adding android before Manifest as following:

android.Manifest.permission.POST_NOTIFICATIONS

It worked for me.

1 Comment

yes, it works for android V13 and above only.
1

Declare in manifest file

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

add this code above onCreate

private ActivityResultLauncher<String> requestNotificationPermission =
            registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
                if (isGranted) {
                    // Permission is granted, perform your action here
                    showLocaleNotification("Dummy Notification");
                } else {
                    Toast.makeText(getActivity(), "Permission denied", Toast.LENGTH_SHORT).show();
                }
            });

then add this code where you show notification

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
            String permission = Manifest.permission.POST_NOTIFICATIONS;
            if (ActivityCompat.checkSelfPermission(getActivity(), permission) == PackageManager.PERMISSION_GRANTED){
                showLocaleNotification("Dummy Notification");
            } else if (shouldShowRequestPermissionRationale(permission)) {

            } else {
                requestNotificationPermission.launch(permission);
            }
        } else {// handle below android 13 , just call showNotification()}

Note: this is just a sample, its better when your app is launched first time take the permission for notification and where you are calling showNotification function just check the sdk version and call showNotification function.

1 Comment

you registred notifications permission callback , but you dismiss to request permission when it is available : String permission = android.Manifest.permission.POST_NOTIFICATIONS; ActivityCompat.requestPermissions(activity,new String[]{permission},req_code)
1

I have tested personally and the key is not only compileSdkVersion. We need to update targetSdkVersion to 33 too in order for the permission to prompt, otherwise it will be defaulted to deny.

Regarding the permission, I have tested personally that we don't need to specify in manifest, as per stated in the documentation: "By default, the FCM SDK (version 23.0.6 or higher) includes the POST_NOTIFICATIONS permission defined in the manifest. " https://firebase.google.com/docs/cloud-messaging/android/client#request-permission13

Anyway, I can't find a way for the FCM permission to work with target API 32 but run the app in API 33 device. So, our app was forced to target API 33 now.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.