未授予com.google.android.c2dm.permission.SEND权限

时间:2018-03-31 10:56:29

标签: java android android-permissions

我在清单中添加了com.google.android.c2dm.permission.SEND,如下所示。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lewwe.broadcastsender">
    <uses-permission android:name="com.google.android.c2dm.permission.SEND" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

但是,当我使用以下代码检查是否授予了权限时,它会返回未授予的结果

    int res = getApplicationContext().checkCallingOrSelfPermission("com.google.android.c2dm.permission.SEND");
    if (res == PackageManager.PERMISSION_GRANTED)
        System.out.println("GRANTED");
    else
        System.out.println("NOT GRANTED");

当我尝试与另一个需要com.google.android.c2dm.permission.SEND权限的应用程序中的另一个广播接收器进行交互时,我收到以下错误日志:

03-31 07:44:21.648 675-691/? W/BroadcastQueue: Permission Denial: broadcasting Intent { flg=0x10 cmp=cn.wps.moffice_eng/cn.wps.moffice.main.gcm.GcmReceiver (has extras) } from com.example.lewwe.broadcastsender (pid=2680, uid=10077) requires com.google.android.c2dm.permission.SEND due to receiver cn.wps.moffice_eng/cn.wps.moffice.main.gcm.GcmReceiver

我想我必须在运行时请求此权限。如果这是真的,我该怎么做?

1 个答案:

答案 0 :(得分:0)

  

com.google.android.c2dm.permission.SEND权限由Google Play服务持有。这样可以防止其他应用调用广播接收器。

https://firebase.google.com/docs/reference/android/com/google/firebase/iid/FirebaseInstanceIdReceiver