5

How to open Google Play in popup like Wisher, Buzzfeed, Vimeo?

Wisher Buzzfeed

I looked at Google documentation, but there is only about opening by Google Play app (market://) or by a browser (http://).

I would like to open inside my instant app in order to install full app like on screens.

Stonek
  • 643
  • 9
  • 14

2 Answers2

7

I think you're looking for showInstallPrompt.

philo
  • 3,580
  • 3
  • 29
  • 40
  • thanks, it's working. But I've a problem with NFC and Instant App. I would like to open Instant App by a NFC tag. On the NFC tag is saved the URL. But when I tap a phone to the NFC tag, a browser is opening. When I click on the same URL via Gmail then Instant App is opening. Do you know why..? – Stonek Jan 17 '18 at 15:37
  • You might want to check out: https://stackoverflow.com/questions/46948624/instant-apps-with-nfc – Kyle Venn Jan 17 '18 at 15:59
  • @KyleVenn I also found it and I've read all answers/comments but there isn't solution about it – Stonek Jan 17 '18 at 16:04
  • It looks like opening from NFC should work on pre-O devices and the Android Oreo bug is being tracked [here](https://issuetracker.google.com/u/0/issues/69368203). – Kyle Venn Jan 17 '18 at 16:07
  • I'm testing on N – Stonek Jan 17 '18 at 16:36
  • I remember that by some time it was working, but after testing, it always opens a browser. I cleared cached data in the browser (from Apps), but it still doesn't work – Stonek Jan 17 '18 at 16:43
  • Please give example. – Pratik Butani Sep 26 '19 at 08:51
5

TL;DR:

InstantApps.showInstallPrompt(activity, 
                              postInstallIntent, 
                              Constants.INSTALL_INSTANT_APP_REQUEST_CODE, 
                              referrerString);

At Vimeo we're using Branch for our web -> mobile app install as well as our instant app -> mobile app install (since it gives some additional metrics and lets us compare the referrers a little better).

If you're interested in using Branch, the documentation for the install prompt can be found here. And the usage looks like:

if (Branch.isInstantApp(this)) {
  myFullAppInstallButton.setVisibility(View.VISIBLE);
  myFullAppInstallButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
       BranchUniversalObject branchUniversalObject = new BranchUniversalObject()
           .setCanonicalIdentifier("item/12345")
           .setTitle("My Content Title")
           .setContentDescription("My Content Description")
           .setContentImageUrl("https://example.com/mycontent-12345.png")
           .setContentMetadata(new ContentMetadata()
                 .addCustomMetadata("property1", "blue")
                 .addCustomMetadata("property2", "red"));

      Branch.showInstallPrompt(myActivity, activity_ret_code, branchUniversalObject);
    }
  });
} else {
  myFullAppInstallButton.setVisibility(View.GONE);
}

Branch's implementation ultimately calls through to the API mentioned in the other answer here.

Which looks like:

public static boolean showInstallPrompt(Activity activity, 
                                        Intent postInstallIntent, 
                                        int requestCode, 
                                        String referrer)

Shows a dialog that allows the user to install the current instant app. This method is a no-op if the current running process is an installed app. You must provide a post-install intent, which the system uses to start the application after install is complete.

You can find an example usage here. And looks like the following:

class InstallApiActivity : AppCompatActivity() {


    /**
     * Intent to launch after the app has been installed.
     */
    private val postInstallIntent = Intent(Intent.ACTION_VIEW,
            Uri.parse("https://install-api.instantappsample.com/")).
            addCategory(Intent.CATEGORY_BROWSABLE).
            putExtras(Bundle().apply {
                putString("The key to", "sending data via intent")
            })

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_install)

        val isInstantApp = InstantApps.isInstantApp(this)

        findViewById<Button>(R.id.start_installation).apply {
            isEnabled = isInstantApp
            // Show the installation prompt only for an instant app.
            if (isInstantApp) {
                setOnClickListener {
                    InstantApps.showInstallPrompt(this@InstallApiActivity,
                            postInstallIntent,
                            REQUEST_CODE,
                            REFERRER)
                }
            }
        }
    }

    companion object {
        private val REFERRER = "InstallApiActivity"
        private val REQUEST_CODE = 7
    }
}

It's not recommended because it's deprecated, but you can technically get the dialog to show using the following code:

InstantApps.showInstallPrompt(activity, Constants.INSTALL_INSTANT_APP_REQUEST_CODE, null);

See also: https://stackoverflow.com/a/47666873/1759443

Kyle Venn
  • 4,597
  • 27
  • 41
  • Do you know of a Java version of this? Java doesn't recognize 'Bundle()', for example. Incredibly I've never seen a Java example of a 'showInstallPrompt' implementation with the associated 'postInstallIntent', like a translation of this reference to the github Kotlin code. – Androidcoder Sep 04 '19 at 15:30