How come a GCM permission isn't granted on And

2019-04-29 09:38发布

问题:

Background

I'm trying to investigate what an app at the office needs to change about its permissions, in order to support Android 6 nicely.

The problem

I've found which permission needs confirmation and which isn't, except for one :

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

It seems that this permission isn't mentioned anywhere that I look for, as one that's not granted automatically and yet I can't find where the user can enable it as a confirmation.

What I tried

In order to find which permissions are granted by default and which aren't , I just called this code:

private void checkPermissionsOfApp(String packageName) {
    PackageManager pm = getPackageManager();
    try {
        final ApplicationInfo applicationInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
        Log.d("AppLog", "Listing all permissions of app with PackageName: " + applicationInfo.packageName);
        PackageInfo packageInfo = pm.getPackageInfo(applicationInfo.packageName, PackageManager.GET_PERMISSIONS);
        //Get Permissions
        String[] requestedPermissions = packageInfo.requestedPermissions;
        if (requestedPermissions != null) {
            for (String permission : requestedPermissions) {
                boolean permissionGranted = pm.checkPermission(permission, packageName) == PackageManager.PERMISSION_GRANTED;
                Log.d("AppLog", "permission:" + permission + " permissionGranted:" + permissionGranted);
            }
        }
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
}

And the call:

checkPermissionsOfApp(getPackageName());

Using the above code, it crashes for the problematic permission, but when using ContextCompat.checkSelfPermission it says it's not granted.

The question

How could it be? How can I grant the app this permission? Is it mentioned anywhere?

回答1:

The documentation, which was updated in October of 2015, still indicates that you need the signature permission.

See also Not receiving push notifications from GCM (Android)

As @CommonsWare mentioned, this does not appear to be part of the new runtime permission checking, or at least is not considered a "dangerous" permission, and so should be automatically granted.



回答2:

ok, I'm not sure why it is this way, but the permission is said to be granted only if :

  1. The package name of the app is its prefix
  2. you also declare the permission, as such:

    <permission
       android:name="com.example.user.androidmtest.permission.C2D_MESSAGE"
        android:protectionLevel="signature"/>
    

The weird thing is that even though the code said that the permission is not granted when not adding the package name, it worked on the app.