1

I'm building an app to create "events", and after you create an event, you can invite people by (among other ways) sending an email with the invitation link.

If the user does not have the app installed, I want a way to allow the user to install the app, and after that, automatically "accept" the invitation.

So, I've read that one way to do this is using com.android.vending.INSTALL_REFERRER receiver, when the user install the app using a link like this: https://play.google.com/store/apps/details?id=com.melorriaga.events&referrer=eventId=1234, the broadcast receiver will be executed and I will be able to read "referrer" and act properly.

I added this in the manifest:

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

and the receiver:

public class InstallReceiver extends BroadcastReceiver {

    private static final String TAG = InstallReceiver.class.getSimpleName();

    public static final String EVENT_ID = "EVENT_ID";

    @Override
    public void onReceive(Context context, Intent intent) {
        String referrer = intent.getStringExtra("referrer");
        Log.i(TAG, "referrer: " + referrer);

        String eventId = getEventIdFromReferrer(referrer);
        Log.i(TAG, "eventId: " + eventId);

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        sharedPreferences.edit()
                .putString(EVENT_ID, eventId)
                .apply();
        }
    }

}

In order to test it, I'm doing:

am broadcast -a com.android.vending.INSTALL_REFERRER --es "referrer" "eventId=1234"

This works when the app is running, or if the app is opened at least once. But, if I install the app doing adb install app.apk, and then am broadcast... (app never opened), the onReceive method is not called. If I open the app and then am broadcast..., now it works.

I've found this: https://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html (from https://stackoverflow.com/a/28322345/1742973), so this seems to be the expected behavior.

So, I'm missing something? what's the point of com.android.vending.INSTALL_REFERRER if the receiver will not be received until the app is manually opened?

The idea is: save the event id in shared preferences after the app is installed, and then, when the app is opened for the first time, check if there is some value stored in shared preferences, if so, "accept the invitation".

Community
  • 1
  • 1
Matias Elorriaga
  • 8,880
  • 5
  • 40
  • 58
  • I've been told that this should be working when the app is installed from the PlayStore, but I want a second opinion... https://www.reddit.com/r/androiddev/comments/5v48ap/weekly_questions_thread_february_20_2017/de8jvar/ – Matias Elorriaga Mar 02 '17 at 22:53
  • Have you solved it? I've got the same problem and I wonder if it's the way the Play Store works or if it's a problem in our code – Yehonatan Apr 05 '17 at 11:20

0 Answers0