2

I am working on app which suggested app. If user installs app successfully using my app then he gets reward in my app. I am getting info about "com.android.vending.INSTALL_REFERRER" action of receiver which provides this but didn't get success...
So please help me with getting some full examples or other suggestion...

This is my code...

Button Click Event

btnInstallApp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent goToMarket = new Intent(Intent.ACTION_VIEW)
                    .setData(Uri.parse("market://details?id=com.idea.backup.smscontacts&referrer=tecksky"));
            startActivity(goToMarket);
        }
    });

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tecksky.referrerdemo">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".activity.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver
        android:name=".receiver.ReferrerCatcher"
        android:exported="true">
        <intent-filter>
            <action android:name="com.android.vending.INSTALL_REFERRER"></action>
        </intent-filter>
    </receiver>
</application>

</manifest>

ReferrerCatcher.java

public class ReferrerCatcher extends BroadcastReceiver {
private static String referrer = "";

@Override
public void onReceive(Context context, Intent intent) {
    referrer = "";
    Bundle extras = intent.getExtras();
    if (extras != null) {
        referrer = extras.getString("referrer");
    }
    Log.e("REFERRER : ", referrer);
   }
}
Tecksky Android
  • 133
  • 3
  • 14
  • What exactly do you mean by "I am getting info about" and "but didn't get success"? Is `ReferrerCatcher.onReceive` called or not? – gus27 Sep 17 '16 at 10:05

1 Answers1

0

Hey Just see how it works I have implemented this in my app as well and works for me perfectly so I am posting you referral code:

Create separate class for receiving reference:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import com.google.android.gms.analytics.CampaignTrackingReceiver;

public class CustomReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
          new CampaignTrackingReceiver().onReceive(context, intent);
          Bundle b= intent.getExtras();
             String referrerString = b.getString("referrer");
            // Log.e("bundle", "bundle= " + referrerString+ " " + referrerString.substring(11, referrerString.length()));
             SharedPrefManager.setPrefVal(context, Constants.REFERRAL, referrerString.substring(11, referrerString.length()));              

    }
}

This class will receive the referral that you have to save somewhere like I am saving it in SharedPrefrence. This broadcast will receive when the user will install app from play store and in "referrer" you will get info that which user pass this user the link to download app then from that referrer key you can give benefit to user who referred your app.

In manifest file add receiver Tag

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

From backend you will get a link from server side for a particular user which you will hit to play store then something like

https://play.google.com/store/apps/details?id=com.example.app&referrer=utm_source=123456ref

Basically in your receiver class you are picking this utm_source and you have to send it to server side when user will sign up in you app. Which means it ll let know that from which reference which id has been generated.

Hope this helps you. Try this and let me know.

Preetika Kaur
  • 1,991
  • 2
  • 16
  • 23
  • Hello, I tried to use your solution but I cannot read the referrer string once the application starts. I use `PreferenceManager.getDefaultSharedPreferences()`. What is the `SharedPrefManager` you use? – fralbo Apr 27 '17 at 12:50
  • Here sharedprefmanager is the custom class for saving info in shared preferences. You can write your own. – Preetika Kaur Apr 28 '17 at 06:48
  • ok, I saw it somewhere else. The key point here is to not use ``getDefaultSharedPreferences()`` to be able to read it back in the app because it refers to a different context than the application (`restrictedContext`). – fralbo Apr 28 '17 at 07:12