How can I respond to the Android broadcast "com.android.vending.INSTALL_REFERRER"?

We are migrating an Android app to Unity and one of the things the Android app does, is register itself as a broadcast receiver through the AndroidManifest like this:

<receiver
    android:name="com.example.OurReceiver"
    android:exported="true" >
    <intent-filter>
        <action android:name="com.android.vending.INSTALL_REFERRER" />
    </intent-filter>
</receiver>

The Android BroadcastReceiver is implemented like this:

public class OurReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String referrer = intent.getStringExtra("referrer");
        // do stuff with the referrer
    }
}

Assuming I have C# code ready to do the stuff with the referrer, how can I do this in Unity?

What you need in order to achieve this:

  1. A custom JAR that contains your Java code (with the BroadcastReceiver).
  2. A custom AndroidManifest.xml like you posted that Unity will use.
  3. Some way of calling from the Java VM back into Unity.

Assuming you have a Java project wuth the BroadcastReceiver, you would need to compile it to JAR. Place the JAR in your Unity project under Assets/Plugins/Android

Also place the AndroidManifest.xml in that folder.

In order to call back into your scripting code from Java, use the UnityPlayer.UnitySendMessage.

See the documentation here on how to use it: Unity - Manual: Create and use plug-ins in Android