I have a Broadcast Receiver for my C2DM(old) Messaging like

    <receiver android:name=".C2DMRegistrationReceiver">
           <!-- Receive the actual message -->
          <intent-filter>
              <action android:name="com.google.android.c2dm.intent.RECEIVE" />
              <category android:name="com.test" />
          </intent-filter>
          <!-- Receive the registration id -->
          <intent-filter>
              <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
              <category android:name="com.test" />
          </intent-filter>
         <intent-filter>
              <action android:name="REGISTRY_RETRY" />
              <category android:name="com.test" />
          </intent-filter>
    </receiver>

for security reasons you should declare a permission for this receiver like

<receiver android:name=".C2DMRegistrationReceiver" permission="com.google.android.c2dm.permission.SEND">

my problem here is that my 3. Intent Filter dont receive the call because i enforce the com.google.android.c2dm.permission.SEND Permission.

So my Question: Is there a way to define 2 Permissions for one Broadcast Receiver or can i enforce the Permission for the Caller inside my onReceive Code?

i tried

  private boolean checkC2DMPermission(Context context) {
    String permission = "com.google.android.c2dm.permission.SEND";
    context.enforceCallingPermission(permission, "Keine C2DM Permission");
    return true;
  }

also i've tested context.checkCallingPermission(permission) its -1 for a C2DM Register Intent. Enforce gave me a SecurityException.

有帮助吗?

解决方案

I know this question is old, but I ran into the same problem, and this comes up in a google search, so here we go.

The short answer is you can't.

The mentioned methods are intended for when you are executing an IPC, for example a bound service using AIDL.

The broadcast receiver is not executing an IPC, so you can't get the senders permissions, from the broadcast receiver.

To enforce the permission, you have to declare the permissions for the broadcast receiver in the manifest. This works because Android enforces the required permissions before it is delivered to the receiver.

The permissions are common for all actions in the receiver. To handle this, you can split the broadcast receiver into separate classes, as @CommonsWare proposes.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top