Android: ActivityCompat.requestPermissions没有显示弹出窗口(Android 13,targetSdkVersion=33)

6

以下编辑=)

在SO上已经有很多关于这个主题的 ,我知道。然而,到目前为止,我找到的所有答案都没有解决我的问题。

问题: ActivityCompat.requestPermissions不会触发弹出窗口,请求用户允许通知权限。

设置:

  • 在物理设备上进行测试(Android 13,Pixel 6)
  • 目标SDK版本为33(targetSdkVersion=33)
  • minSdkVersion = 29
  • AndroidManifest具有权限:<uses-permission android:name="android.permission.POST_NOTIFICATION" />
  • 在一个正常工作的Activity的onCreate中,我执行:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_DENIED)
{
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.POST_NOTIFICATIONS}, 1);
}

回调函数的格式如下:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case 1:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(MainScreenActivity.this, "Woho, you have enabled notifications!", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainScreenActivity.this, "Ouch, this is gonna hurt without notifications", Toast.LENGTH_SHORT).show();
            }
            return;
    }
}

这里是 build.gradle 文件:

TheApp\build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        maven { url 'https://raw.github.com/xujiaao/mvn-repository/master/releases' }
        maven {
            url 'https://jitpack.io'
        }
        maven { url 'http://dl.bintray.com/ahmedrizwan/maven'
            allowInsecureProtocol = true
        }
        google()

    }
    dependencies {
        classpath 'com.google.gms:google-services:4.3.10'  // Google Services plugin
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.5.2'
        classpath 'com.android.tools.build:gradle:7.3.1'
        classpath "io.realm:realm-gradle-plugin:10.0.0"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local'
            allowInsecureProtocol = true
        }
        google()
        jcenter()
        flatDir {
            dirs 'src/main/libs'
        }
    }
}

TheApp\app\build.gradle


apply plugin: 'com.android.application'
apply plugin: 'realm-android'
apply plugin: 'com.google.firebase.crashlytics'

android {
    compileSdkVersion 33
    lintOptions {
        abortOnError false
    }

    buildFeatures{
        dataBinding = true
    }

    defaultConfig {
        applicationId "test.myapp"
        minSdkVersion 29
        targetSdkVersion 33
        versionCode 60
        versionName "1.60"

    }
    buildTypes {

        debug {
            signingConfig signingConfigs.debug_keystore
            aaptOptions.setProperty("cruncherEnabled", false)
        }

        release {
            signingConfig signingConfigs.release_keystore
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            aaptOptions.setProperty("cruncherEnabled", false)
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    namespace 'test.myapp'
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.cardview:cardview:1.0.0'

    implementation 'com.google.firebase:firebase-core:17.5.1'
    implementation 'com.google.firebase:firebase-messaging:20.3.0'

    implementation 'com.google.code.gson:gson:2.8.6'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'com.google.android.gms:play-services-gcm:17.0.0'
    implementation 'com.google.android.gms:play-services-location:17.1.0'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'

    implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
    implementation "androidx.preference:preference-ktx:1.1.1"
    implementation 'com.google.firebase:firebase-crashlytics:17.2.2'
    implementation 'com.google.firebase:firebase-analytics:17.6.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
    implementation 'com.google.android.play:app-update:2.0.1'

    def fragment_version = "1.5.4"

    // Java language implementation
    implementation "androidx.fragment:fragment:$fragment_version"
    // Kotlin
    implementation "androidx.fragment:fragment-ktx:$fragment_version"
    // Testing Fragments in Isolation
    debugImplementation "androidx.fragment:fragment-testing:$fragment_version"
}
apply plugin: 'com.google.gms.google-services'  // Google Play services Gradle plugin


我通过调试��逐步执行了代码,并确认requestPermissions已被执行,但是onRequestPermissionsResult立即触发,设备上没有弹出窗口。

我还阅读了this,但我认为这是由于开发人员未按照here中描述的选择加入模型,但也许,这真的是一个问题吗?因为我无法理解这个...

更新

我在应用程序的其他部分请求其他权限,这行代码运行良好-我得到弹出窗口以授予权限:

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, BaseActivity.PHONE_STATE_PERMISSION_CODE);

更新 2

在代码库的另一部分,我有相同的 requestPermission 调用,但是针对另一个权限,这个调用起作用了:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, BaseActivity.PHONE_STATE_PERMISSION_CODE);
}

当执行此代码时,我会得到以下结果: enter image description here 但是,如果我将requestPermissions调用更改为以下内容,则不会发生任何事情,并且我立即进入回调函数,显示“rejected”:
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.POST_NOTIFICATIONS}, BaseActivity.PHONE_STATE_PERMISSION_CODE);
}

图片:

在此输入图像描述

更新3

我也尝试使用新的ActivityResultLauncher,但行为是相同的 - 它直接转到回调而没有显示任何弹出窗口。


当前支持/兼容的AndroidX库是什么? - snachmsm
@blackapps,这不是文档所说的内容... - Ted
1
我就快要收工了;-) - blackapps
我看了那个,但据我所知它是可选的?而另一个版本似乎更容易实现(没有找到其他版本的示例,我认为它被称为PermissionRequest)... - Ted
要进行测试,请参考https://developer.android.com/develop/ui/views/notifications/notification-permission#test - ecle
显示剩余16条评论
2个回答

4

我真是太糊涂了。在上面的问题中,我已经发布了AndroidManifest包含的内容,并写道:

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

这是不正确的。我漏掉了一个 s,应该是:

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

糟糕!:-)


1
很高兴你发现你错过了字母S。在我的情况下,我已经包含了S,即<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>我的应用程序是基于ReactNative构建的,我在AndroidManifest中添加了权限,并请求授权,但没有显示弹出。我正在使用带有Google Play商店的Android 13模拟器。 - rjkolli7
你使用哪种方法来请求权限? - Ted
我犯了更愚蠢的错误:完全忘记在清单中声明此权限。浪费了30分钟的时间。这将是Android抛出异常以节省我们所有人麻烦的完美场所。 - Vasiliy

0

更新你的 AndroidX 依赖库,只有最新的库才有处理最新权限引入的代码。

替换

implementation 'androidx.appcompat:appcompat:1.2.0'

使用

implementation 'androidx.appcompat:appcompat:1.5.1'

通常尝试更新(并合并)AndroidX库版本


谢谢,我已经更新了它,同步了Gradle,构建并调试部署到设备上 - 不幸的是还是一样的问题 =( - Ted
1
仅基于我的问题,目前我在我们的代码片段中没有看到任何差异,也没有其他想法... - snachmsm
我们都错过了一个不同之处:AndroidManifest中缺少一个“S”。 - Ted
1
很高兴你在几个小时/几天的时间里绞尽脑汁之后找到了答案 :) 祝好运! - snachmsm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接