In my application have a referral program.I want when a customer share app link, that link redirect to playstore and after install app referral code retrieve from playstore. I already do that link but I don't understand how can i receive that referral code after install.

public static void shareMyApp(Context context) {

    String link = "https://play.google.com/store/apps/details?id="+context.getPackageName();

    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType("text/*");
    sharingIntent.putExtra(Intent.EXTRA_TEXT, link);
    context.startActivity(Intent.createChooser(sharingIntent, "Share Via"));
}

2 Answers

1
Ujjval Shrimali On

For that, you have to use below the library

implementation 'com.android.installreferrer:installreferrer:1.0'

https://developer.android.com/google/play/installreferrer/library

https://developer.android.com/training/app-links/deep-linking.html

Thanks, it may help you

1
Sagar Vekariya On

Create a link with referral code and send to others friends and users...

String link = http://yourdomain.com/testrefer.php?refercode=ABCD // http or https

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.EXTRA_TEXT, link);
context.startActivity(Intent.createChooser(sharingIntent, "Share Via"));

and in testrefer file to create app store link and append refercode and then redirect to play store using below link and then you can get referral code.

https://play.google.com/store/apps/details?id="+context.getPackageName()+"&referrer=ABCD

in Manifest file in your launching screen to write below code

<activity
    android:name=".activity.SplActivity"
    android:screenOrientation="portrait"
    android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data
                android:host="yourdomain.com"
                android:pathPrefix="/testrefer.php"
                android:scheme="https" />  <!-- https or http -->
      </intent-filter>
</activity>

InstallRefererReciever is a file name that you have created BroadcastReceiver

<receiver
    android:name=".common.InstallRefererReciever" 
    android:exported="true"
    android:permission="android.permission.INSTALL_PACKAGES">
    <intent-filter>
        <action android:name="com.android.vending.INSTALL_REFERRER" />
    </intent-filter>
</receiver>

Create BroascatReceiver and write below code :

public class InstallRefererReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null) {
          Bundle extras = intent.getExtras();
          if (extras != null) {
             String referral = extras.getString("referrer");
             Logger.e("ReferCode --> ", referral);
             if (referral != null) {
                 if (!referral.equals("utm_source=google-play&utm_medium=organic")) {
                     if (!referral.equals("com.android.chrome")) {
                       String referralCode = referral; // Store in sharedpreferences
                    }
                }
             }
          }
       }
   }


}