简体   繁体   中英

How do I request push notification permissions for android 13?

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?

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"/>

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. 摇篮设置

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM