100

我知道,关于这个话题的问题已经有many many个了.然而,到目前为止,我找到的答案都不能解决我的问题.

问题是: ActivityCompat.requestPermissions不会触发弹出窗口,要求用户授予通知权限.

设置:

  • 物理设备测试(Android 13、Pixel 6)
  • 目标SDK版本33(Target SdkVersion=33)
  • MinSdkVersion=29
  • AndreidManifest拥有权限:<uses-permission android:name="android.permission.POST_NOTIFICATION" />
  • 在onCreate in a Work Activity中,我这样做:
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的Opt-in模型,但也许,这毕竟是一个问题?因为我搞不懂这件事...

更新

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

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

更新 2

在代码库的另一部分中,我有相同的questPermission调用,但需要另一个权限,这就像一个咒语:

@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);
}

When this is executed, I get this: enter image description here

但是,如果我更改了请求权限调用,使其看起来像这样,则不会发生任何事情,并且我会立即进入回调,并带有一个"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);
}

Picture: enter image description here

更新 3

我也试过使用新的ActivityResultLauncher,但它的行为是一样的-它直接转到回调,不显示任何弹出窗口.

推荐答案

愚蠢的我.在上面的问题中,我确实发布了androidManifest包含的内容,我写道:

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

这是不正确的.我错过了一个s,应该是:

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

哎呀:-)

Android相关问答推荐

Jetpack Compose make父级图像填充高度

编写Landscape BoxWithRequests具有错误的高度.(aspectRatio matchHeight约束第一次未按预期工作)

懒惰的垂直网格中盒子的重量-Jetpack组合

组成底部导航栏,自定义形状,周围透明

Android Compose Pages 3-一次加载所有页面,无需在LazyColumn中滚动,无需网络调用和内部滚动

在Android Studio中,如何在BuildSrc Dependenices Kotlin文件中指定时标记与旧版本的依赖关系

在Android 14/SDK 34中使用RegisterReceiver的正确方式是什么?

Android App Google AdMob";广告加载失败:3;带有测试ID,&q;广告加载失败:1 for My Gahad

找不到com.android.tools.build:gradle:8.0

从片段导航回来

如何在Jetpack Compose中向SearchBar添加边框

Android 构建失败:找不到 flexbox2.0.1.aar

请求访问小部件中的位置权限

Jetpack Compose with Paging 3 发出太多网络请求

Jetpack Compose UI - 在 AlertDialog 中单击时按钮宽度会发生变化

如何在行/列/卡片 compose 中添加左边框

Android Compose 创建抖动动画

使用 Jetpack Compose 时,如何以简单的方式在 Color.kt 中定义 colored颜色 ?

从expose 的 dropdownMenu 可组合、jetpack 组合中 Select 选项时,不会触发文本字段的 onValueChange

如何使用文件提供程序将视频从一个应用程序共享到另一个应用程序?