Search code examples
androidbroadcastreceiverandroid-manifestandroid-permissionsandroid-security

Android Broadcast Reciever with permissionLevel signature not receiving broadcasts


I have 2 Applications signprotectbroadcast and broadcastsender

In signprotectbroadcast I have registered a Receiver

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.signprotectbroadcast">
<permission android:name="PERMISSION_OP"
    android:protectionLevel="signature"
    android:label="PERMISSION">
</permission>

<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/Theme.SignProtectBroadcast">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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


    <receiver android:name=".MyReciever"
        android:enabled="true"
        android:exported="true"
        tools:ignore="ExportedReceiver"
        android:permission="PERMISSION_OP">
        <intent-filter>
            <action android:name="ACTION_OP" />
        </intent-filter>
    </receiver>
</application>
</manifest>

in the application broadcastsender I request the permission in the Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastsender">
<uses-permission android:name="PERMISSION_OP"/>
<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/Theme.BroadCastSender">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

and send the broadcast like this

    sendBtn.setOnClickListener {
        val intent = Intent()
        intent.action = "ACTION_OP"
        intent.component = ComponentName("com.example.signprotectbroadcast", "com.example.signprotectbroadcast.MyReciever" )
        sendBroadcast(intent, "PERMISSION_OP")
    }

I have also made one common keystore when running a release build variant but this is not working at all, tried everything.

As soon as I remove the permission from the receiver block as well as sendBroadCast function the broadcast is being received properly.

Can someone point me in right direction where to debug why this broadcast is not received or how to debug this?


Solution

  • The app that sends the broadcast needs to declare the permission, as well as state it uses the permission;

    Sending app : broadcastsender (declare and use the permission)

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.broadcastsender">
    
        <permission android:name="PERMISSION_OP"
        android:protectionLevel="signature"
        android:label="PERMISSION"/>
    
        <uses-permission android:name="PERMISSION_OP"/>
    
        <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/Theme.BroadCastSender">
            <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>
    

    Usage :

    sendBtn.setOnClickListener {
            val intent = Intent()
            intent.action = "ACTION_OP"
            intent.component = ComponentName("com.example.signprotectbroadcast", "com.example.signprotectbroadcast.MyReciever" )
            sendBroadcast(intent, "PERMISSION_OP")
        }
    

    Receiving app : signprotectbroadcast (uses the permission only)

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.signprotectbroadcast">
    
    <uses-permission android:name="PERMISSION_OP"/>
    
    <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/Theme.SignProtectBroadcast">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    
    
        <receiver android:name=".MyReciever"
            android:enabled="true"
            android:exported="true"
            tools:ignore="ExportedReceiver"
            android:permission="PERMISSION_OP">
            <intent-filter>
                <action android:name="ACTION_OP" />
            </intent-filter>
        </receiver>
    </application>
    </manifest>